Cheatsheet: Undoing things
Voice anchor
Section titled “Voice anchor”Git stores snapshots. Every other command is just navigating those snapshots.
Recovery map (memorize this)
Section titled “Recovery map (memorize this)”| Scenario | Command |
|---|---|
| Discard working-directory changes | git restore <file> |
| Unstage a staged file | git restore --staged <file> |
| Fix last commit (message or content) | git commit --amend [-m "new message"] |
| Undo local-only commit, keep changes staged | git reset --soft HEAD~1 |
| Undo local-only commit, keep changes in working dir | git reset HEAD~1 |
| Undo local-only commit, discard everything | git reset --hard HEAD~1 |
| Undo pushed commit (safe) | git revert <commit-hash> |
| Recover from destructive operation | git reflog then git reset --hard <hash> |
Reset mode mechanics
Section titled “Reset mode mechanics”| Mode | Commit pointer | Staging | Working dir | Use case |
|---|---|---|---|---|
--soft HEAD~1 | Back 1 | KEPT | KEPT | ”Redo commit with different message or more files” |
--mixed HEAD~1 (default) | Back 1 | CLEARED | KEPT | ”Re-stage piece by piece, maybe as multiple commits” |
--hard HEAD~1 | Back 1 | CLEARED | CLEARED | ”Throw it all away”, destructive, use reflog if regret |
HEAD~N means N commits back. HEAD alone means current.
The PUSHED RULE (memorize)
Section titled “The PUSHED RULE (memorize)”Has the commit been pushed?
- No then
git resetorgit commit --amendare safe (you’re rewriting history that only you have) - Yes then use
git revert; nevergit reset+git push --force
git reflog mini-tutorial
Section titled “git reflog mini-tutorial”After any destructive operation, run:
git reflogYou see output like:
abc1234 HEAD@{0}: reset: moving to HEAD~3def5678 HEAD@{1}: commit: Add feature Xghi9012 HEAD@{2}: commit: Refactor parserTo recover:
git reset --hard HEAD@{1} # via reflog reference# orgit reset --hard def5678 # via commit SHAThe reflog stores ~90 days. If you realize a week later, you’re fine. If you realize three months later, hope someone else has it.
What’s in L5
Section titled “What’s in L5”L5 starts Phase 2 with branches, parallel sequences of snapshots. The snapshot mental model becomes the most useful here because branches are just pointers into the snapshot graph. You learn git branch, git checkout / git switch, and git merge. Branches are how you collaborate.