The Hook (The "Byte-Sized" Intro)
"Undo" and "recovery" sound the same but solve different problems. Undo fixes the present — you don't want what you just did. Recovery rescues the past — you lost something and need it back. Mixing them up leads to making the situation worse. Know which scenario you're in, and you'll pick the right tool instantly.
📖 What is Undo vs Recovery?
Undo operations remove recent changes intentionally. Recovery operations find and restore lost work. Different tools serve each purpose.
Conceptual Clarity
| Aspect | Undo | Recovery |
|---|---|---|
| Goal | Remove a recent change | Find and restore lost work |
| Trigger | "I don't want this" | "Where did my work go?" |
| Urgency | Low — you're in control | High — something is missing |
| Tools | reset, revert, restore, amend | reflog, fsck, cherry-pick |
| Risk | Low if you choose the right tool | Low once you find the commit |
Decision matrix:
| Situation | Type | Tool |
|---|---|---|
| Bad commit message | Undo | git commit --amend |
| Wrong file committed | Undo | git restore --staged + amend |
| Last commit was wrong | Undo | git reset --soft HEAD~1 |
| Pushed commit was wrong | Undo | git revert <sha> |
Accidental reset --hard | Recovery | git reflog + reset |
| Deleted branch | Recovery | git reflog + branch |
| Lost stash | Recovery | git reflog or fsck |
| Need one commit from another branch | Recovery | git cherry-pick <sha> |
Real-Life Analogy
- Undo = Erasing a mistake on a whiteboard while the marker is still in your hand
- Recovery = Searching the trash can for papers you accidentally threw away yesterday
Visual Architecture
Why It Matters
- Clarity: Knowing whether you need "undo" or "recovery" narrows your tool choice instantly.
- Speed: The right mental model means faster problem resolution.
- Safety: Using undo tools for recovery (or vice versa) can make things worse.
Code
# ─── UNDO scenarios ───
git commit --amend -m "Fixed message" # Fix last commit
git reset --soft HEAD~1 # Undo last commit
git revert abc123 # Undo pushed commit
git restore file.js # Discard file changes
git restore --staged file.js # Unstage a file
# ─── RECOVERY scenarios ───
git reflog # Find lost commits
git reset --hard HEAD@{3} # Restore to past state
git branch recovered abc123 # Recreate deleted branch
git cherry-pick def456 # Copy a specific commit
git fsck --unreachable | grep commit # Find dangling commitsKey Takeaways
- Undo = intentionally removing a change you don't want.
- Recovery = finding and restoring work that was accidentally lost.
- Ask yourself: "Do I want to remove something, or find something?" — then pick the right tool.
- Both are normal, routine parts of Git workflow — not emergencies.
Interview Prep
-
Q: What is the difference between
git revertandgit reflogin terms of use case? A:git revertis an undo tool — it creates a new commit that reverses a previous commit you don't want.git reflogis a recovery tool — it shows every position HEAD has been, letting you find and restore lost commits. -
Q: How would you undo vs recover from a
git reset --hard HEAD~3? A: If intentional (undo), you're done — the reset removed the 3 commits. If accidental (recovery), usegit reflogto find the commit hash before the reset, thengit reset --hard <sha>to restore. -
Q: When would you use
cherry-pickas a recovery tool? A: When a specific commit exists on another branch (or in reflog) and you need to bring just that one change to your current branch — without merging the entire branch.