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 pushgit 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 mergedUndoing things
| Mistake | Fix |
|---|---|
| Wrong commit message | git commit --amend |
| Committed too early | git reset --soft HEAD~1 |
| Want to discard local changes to a file | git restore <file> |
| Need to park work briefly | git 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 whenThe escape hatch
git reflogEvery position HEAD has been in, including states you thought you destroyed.
If you have lost a commit, it is almost certainly here.