Useful references
Git LFS commands
git lfs track "*.psd"
git add .gitattributes
- See more: https://git-lfs.github.com/
Push
git push --set-upstream <remote> <local branch> # creates remote branch with same name
Reset/Revert/Checkout
The best reference is Atlassian Git Reset/Revert/Checkout
The 3 trees are: 1. working 2. staging 3. commit history
checkoutworks on the HEAD pointer only, so switching branches or changing filesresetworks on both HEAD and the branch pointer, three flavors--soft,--mixed (default),--hard.
Reset file to commit
git checkout <hash> -- <file1> <file2> # Reset files to a certain hash, defaults to head
Cherry-Pick
git cherry-pick is used when you want to selectively apply a commit to your current branch.
git cherry-pick -x <commit-hash> - The -x option is to add a message saying which commit was applied. The commit hash is the commit you wish to apply to the current branch.
Stashing
git stash- runsgit stash pushand saves a local snapshotgit stash show- shows the modifications of the most recent stash..git stash list- shows the stack of stashes of changes made.git stash pop- defaults to applying the top stash on the stack. Inverse ofgit stash push
Clean
git clean -n- get rid of untracked, dry rungit clean -f- actually remove them
Diff
git diff- shows differences of the working tree and HEADgit diff --staged- shows differences of the staged files and HEAD
Tags
- Git tag
Branches
git checkout -b <new-branch-name> <start-point>git fetch <remote> <branch>- leaving off the branch gets all branches
Log
With the zsh aliases.
glol- view all the changesglola- view all, even included the fetched branchesglol master..upstream/master- see all the commits between master and the upstream/master branch.
Show
Show a particular git object, tree or commit
git show # shows most recent commit
git show -5 <filename> # Last 5 commits that changed a particular file
git show --name-only <commit-hash> # see only the files changed in the commit
Merge
git merge origin/master
Pull
git pull
Fetch
Refspec
The refspec looks like this
fetch = +refs/heads/master:refs/remotes/origin/master
This says that with fetch, by default will only update the master ref to. The format is <src>:<dst>, in which we look on the remote server for <src>, and track them locally in <dst>. + says if it's not a fast forward, do it anyway.
Hence, for an example command (the first is short for the second)
git fetch origin master:refs/remotes/origin/master
git fetch origin refs/heads/master:refs/remotes/origin/master
Looks on the origin remote, for a branch called master, and writes the hashcode into the file .git/refs/remotes/origin/master. That is, it's tracked by origin/master.
We can also look on the remote server for checking out a certain pull request. In order to see all the refs on a remote server, we can run,
git ls-remote <remote>
Common Workflows
Working on common repository
- Make a fork on Github and clone it locally
- Setup the original as a remote repository. origin should be your copy of it
git remote add upstream https:github.com/user/project.git
- Update the repository occasionally
git fetch upstream
Other helpful commands
glola # In Zsh alias to see log with everything in refs
Examining a Pull Request snapshot
Method 1 is probably the easiest
- Create branch
pr40locally from pull request refspec on the remote server upstreamgit fetch upstream pull/40/head:pr40
- Checkout the pr
git checkout pr40
Other helpful commands
git ls-remote <remote> # view all refspecs on remote server
Method 2 is to pull directly from other person's branch. The other way to work on the project is to pull changes from the user's copy of the repo.
- Fork the repo
- Clone the repo
- Create some branch to hold the pull request
- Pull the merge request branch into your local repository on the newly created branch
Updating a Pull Request
Note that you must have proper permission to push onto someone else's branch. The forks of the original repository will copy the permissions of the original repo. Otherwise you could make a pull request to someone else's forked repo in order for the changes to be tracked on the original pull request.
- Push the changes onto their forked copy, with the same branch name.
Making a Pull Request
- Make a fork of the repository
Working with Dotfiles
The setup steps are creating a bare repository with the $HOME directory is the working tree.
git init --bare $HOME/.cfg
alias config='/usr/bin/git --git-dir=$HOME/.cfg/ --work-tree=$HOME'
config config --local status.showUntrackedFiles no
echo "alias config='/usr/bin/git --git-dir=$HOME/.cfg/ --work-tree=$HOME'" >> $HOME/.bashrc
The usage of this is as follows:
config ls-files # See files under git control
config status # everything else works normally
config add path_to_file
config remote -v
config push
Managing a Web Server on a live server
Might be worth looking into to set up the SGSA website this way.
Merge Conflicts
Undoing previous commit
git commit --ammend- makes another commit, but modifies the previous commit