The everyday loop
Ninety percent of Git work is five commands: status to see where you are, add to stage, commit to record, pull --rebase to sync, and push to publish. The other ten percent — the part this guide covers — is recovering from mistakes and understanding what's actually staged.
Staging precisely
git add -p— stage hunks interactively; split one file's changes into multiple commitsgit restore --staged file— unstage without touching the working treegit restore file— discard uncommitted changes in one file (destructive)git stash -u— stash including untracked files;git stash popto restore
Undoing safely
The key question before any undo: have you pushed? If yes, prefer git revert <sha> — it creates a new commit that inverses the old one, keeping history honest for everyone. If the mistake is still local, you can rewrite:
git commit --amend— fix the last commit's message or contentsgit reset --soft HEAD~1— undo last commit, keep changes stagedgit reset --hard HEAD~1— undo last commit and delete the changesgit reflog— every HEAD movement for 90 days; the way back from "disasters"
Branching and inspection
git switch -c feature/x— create and switch (the moderncheckout -b)git log --oneline --graph --all— the shape of historygit diff main...HEAD— your branch vs where it forkedgit blame -L 20,30 file— who last touched those lines and in which commitgit bisect start— binary-search history for the commit that introduced a bug
Two habits worth building
Commit messages in the imperative: "add retry loop", not "added retry loop" — the convention that makes git rebase auto-squash messages read correctly. Never rewrite public history: push --force on a shared branch destroys teammates' work; if you must, use --force-with-lease, which refuses when someone pushed before you.
Keep the full searchable reference open in the Git Cheat Sheet — it runs entirely in your browser.