All cheat sheets

Git commands worth memorising

The dozen commands that cover almost everything, plus how to undo the three mistakes everyone makes.

1 min read

Daily

git status                    # what has changed
git diff                      # what changed, in detail
git add -p                    # stage selectively, hunk by hunk
git commit -m "message"
git pull --rebase             # get others' work without a merge commit
git push

git add -p is the one worth learning properly. It walks you through each change and asks whether to stage it, which makes small, coherent commits easy.

Branching

git switch -c my-branch       # create and switch
git switch main               # switch back
git branch -d my-branch       # delete once merged

Undoing things

MistakeFix
Wrong commit messagegit commit --amend
Committed too earlygit reset --soft HEAD~1
Want to discard local changes to a filegit restore <file>
Need to park work brieflygit stash then git stash pop

Seeing what happened

git log --oneline -20
git log -p <file>             # history of one file, with diffs
git blame <file>              # who last touched each line, and when

The escape hatch

git reflog

Every position HEAD has been in, including states you thought you destroyed. If you have lost a commit, it is almost certainly here.