Lesson Completion
Back to course

Undo vs Recovery

Beginner
8 minutes4.7Git

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

AspectUndoRecovery
GoalRemove a recent changeFind and restore lost work
Trigger"I don't want this""Where did my work go?"
UrgencyLow — you're in controlHigh — something is missing
Toolsreset, revert, restore, amendreflog, fsck, cherry-pick
RiskLow if you choose the right toolLow once you find the commit

Decision matrix:

SituationTypeTool
Bad commit messageUndogit commit --amend
Wrong file committedUndogit restore --staged + amend
Last commit was wrongUndogit reset --soft HEAD~1
Pushed commit was wrongUndogit revert <sha>
Accidental reset --hardRecoverygit reflog + reset
Deleted branchRecoverygit reflog + branch
Lost stashRecoverygit reflog or fsck
Need one commit from another branchRecoverygit 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

flowchart TD PROBLEM["Something went wrong"] --> TYPE{"What happened?"} TYPE -->|"Made a bad change"| UNDO["🔄 UNDO<br/>reset, revert, amend"] TYPE -->|"Lost something"| RECOVER["🔍 RECOVER<br/>reflog, fsck, cherry-pick"] style UNDO fill:#1a1a2e,stroke:#ffd700,color:#ffd700 style RECOVER fill:#0f3460,stroke:#53d8fb,color:#53d8fb

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

bash
# ─── 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 commits

Key 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 revert and git reflog in terms of use case? A: git revert is an undo tool — it creates a new commit that reverses a previous commit you don't want. git reflog is 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), use git reflog to find the commit hash before the reset, then git reset --hard <sha> to restore.

  • Q: When would you use cherry-pick as 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.

Topics Covered

Git RecoveryGit Fundamentals

Tags

#git#undo#recovery#beginner-friendly

Last Updated

2026-02-13