Git, the useful parts
Most of Git is rarely needed. This page keeps the commands that save real time every week — and the ones that rescue you when something went wrong.
Clean history before review rebase
Commit freely while working, then shape the branch into steps a reviewer can follow.
git commit --fixup=abc123 # "belongs to that commit"
git rebase -i --autosquash main # fixups are folded in automatically
git push --force-with-lease # refuses if someone pushed meanwhile
Never rewrite history that others already built on; --force-with-lease is the safe default when you must.
Nothing is really lost reflog
Every position of HEAD is recorded for about 90 days, including commits a bad reset or rebase seemed to delete.
git reflog # find the state before the mistake
git reset --hard HEAD@{4}
git branch rescue a1b2c3d # or keep it on a new branch
Find the commit that broke it bisect
Binary search over history: 1000 commits need about ten checks. With a test script Git does the whole search for you.
git bisect start
git bisect bad # current version is broken
git bisect good v2.4.0 # this one worked
git bisect run ./test.sh # exit 0 = good, 1 = bad
git bisect reset
Two branches at once worktree
Check out a second branch in another directory instead of stashing half-finished work to fix a bug.
git worktree add ../hotfix release/2.4
git worktree list
git worktree remove ../hotfix
Clearer everyday commands switch
checkout did too many things. Newer commands split them:
git switch -c feature/login # new branch
git restore src/app.py # drop local changes in a file
git restore --staged src/app.py # unstage, keep changes
Resolve a conflict once rerere
"Reuse recorded resolution" remembers how you fixed a conflict and replays it on the next rebase of the same branch.
git config --global rerere.enabled true
Team conventions that pay off habits
Short-lived branches merged within days. Commit messages that say why, with a subject under about 50 characters. A .gitattributes with * text=auto to stop line-ending noise between Windows and Linux. Signed commits (gpg.format ssh works with your SSH key) for anything that deploys.
Big repositories scale
Partial clone downloads file contents only when needed; sparse checkout limits the working tree to the directories you touch.
git clone --filter=blob:none URL
git sparse-checkout set services/api docs
Written against Git 2.40 and newer. Older versions lack switch/restore and SSH signing.