Some Advanced Git Tips
Recently I learnt Git In-depth in Frontend Masters by Nina, it was about 4 hours course, I tweeted some notes while learning and I think it is nice to convert them into a blog post.
Most useful
git add -pwill add your changes in chunks, like what SourceTree is able to do, butgit add -pis more powerful, you can even divide chunks into smaller pieces.git show <SHA>will show all the details of a commit, which I did not know before.git merge --no-ffwill merge with a merge commit, even if there is no conflict and git is able to fast forward, and I finally learnt what does fast forward means.git config rerere.enabled trueno more repeat same merge conflict again, resolve once and git will remember it and automatically does the same for you next time.git branch --merged=masterfind all branches that have already been merged into master, it is very handy when you want to do some cleanups.git stash --include-untrackedwill stash untracked files as well.git commit --fixup <SHA>combined withgit rebase -i --autosquash <SHA>~is awesome! I was doing some random commit message and squash usinggit rebase -i, now I don't need to write random commit message any more!git clean -dwill clean untracked files and directories. You need to combine with-fto do actual clean or--dry-runto see what will be cleaned.git reset --merge ORIG_HEADwill undo a merge.
Some random tips
cat .git/HEADwill show where the current HEAD is.git stash save 'message'will stash with messages.git show-refwill show hashes of all local and remote branches and tags.HEAD~is same asHEAD~1, and try not to use^, it is most likely not what you want.git log --name-status --follow filenamewill show all history of that file
Get Hu Chen’s stories in your inbox
Join Medium for free to get updates from this writer.
git reset vs git checkout
This might be the most confusing part of git. Normally we use git checkout to checkout a branch, or certain commits, and use git reset to undo a commit. But it is more than that.
git checkout
- If you use
git checkouton a file, that isgit checkout <SHA> -- filename, serves as a way to revert back to an old version of an individual file. git checkout HEAD -- filenamewill reset a file to HEAD version, no matter whether you have staged the changes of the file.git checkout -- filenamewill remove local changes which are not staged, but not make change in staging area.
git reset
git reset -- filenamewill move a file out of staging area, which means unstage a file.git reset -- filenameis same asgit reset HEAD -- filename.git reset <SHA> -- filenamewill update the file in staging area, and keep working area unchanged.git reset <SHA> -- filename+git checkout -- filename=git checkout <SHA> -- filename.git reset <SHA>will move HEAD to the commit, and keep diff of current version and commit version in working area.git reset --soft <SHA>will move HEAD to the commit, and keep diff of current version and commit version in staging area. As if you make a change, stage it and make a commit, you can consider this command one step backward.git reset --hard <SHA>will move HEAD to the commit, and discard changes.
Conclusion
You can find the full slides and tips here, it is the place I will constantly check back.








