Skip to content

Summary: Undoing things

Six commands. Each maps to a different scenario:

ScenarioCommand
Discard working-directory changesgit restore <file>
Unstage a staged filegit restore --staged <file>
Fix the last commit (local only)git commit --amend
Undo a local-only commitgit reset (with —soft / —mixed / —hard)
Undo a pushed commitgit revert <commit>
Recover from destructive operationsgit reflog + git reset --hard <hash>
ModeCommit pointerStaging areaWorking directory
--softMoves backKEPTKEPT
--mixed (default)Moves backCLEAREDKEPT
--hardMoves backCLEAREDCLEARED (destructive)
  • Reset rewrites history. Use only for local-only commits.
  • Revert adds new history (an inverse commit). Use for anything pushed.
  • Wrong choice: running git reset on pushed history. Wrong because it forces other team members into ugly merge resolution.
  • Records every HEAD movement for ~90 days.
  • Lets you recover from git reset --hard, lost branches, bad force-pushes, etc.
  • Only exists on your machine. Cannot recover what was never on your machine.
  • The discipline: before you panic, run git reflog.

You have:

  • Mental model (L1)
  • Commands and discipline (L2 + L3)
  • Recovery (L4)

That is confident solo git workflow. Phase 2 adds collaboration.

The recovery toolkit. Every time you experiment in Phase 2 (branches, merges, conflicts), the safety net from L4 is what lets you experiment without fear.

Git stores snapshots. Every other command is just navigating those snapshots.

Undoing is just navigating to a different snapshot. Recovery is just remembering which snapshot you wanted. Almost nothing is truly lost.