Undoing things
What this lesson covers
Section titled “What this lesson covers”L4 closes Phase 1 by teaching recovery. The mechanics from L2, the discipline from L3, and the recovery toolkit from L4 together give a learner confident solo git workflow.
Three load-bearing ideas:
-
Each “undo” maps to one area of the three-area model from L2. Working directory undo is
git restore. Staging undo isgit restore --staged. Repository undo splits intogit reset(local-only history) andgit revert(safe for pushed history). -
The reset vs revert distinction matters for teams. Reset rewrites history; revert adds inverse history. Reset is fine for local-only commits. Revert is the only safe option for pushed commits.
-
git reflogis the safety net. Almost nothing in git is truly lost. The reflog records every HEAD movement for 90 days. Recovery from a destructive operation is onegit reflog+ onegit reset --hard <hash>away.
By the end of L4, the reader will be able to
Section titled “By the end of L4, the reader will be able to”- Discard working-directory changes with
git restore <file> - Unstage staged files with
git restore --staged <file> - Edit the last commit’s message or contents with
git commit --amend - Undo local-only commits with
git reset(and choose between —soft, —mixed, —hard based on what they want to keep) - Undo pushed commits safely with
git revert - Recover from destructive operations using
git reflog - Articulate why teams converge on “rewrite freely on personal branches, never rewrite on shared branches”
Prerequisites
Section titled “Prerequisites”- L1 (snapshot mental model)
- L2 (three-area model +
git init/git status/git add/git commit) - L3 (commit hygiene)
- Comfort with the command line
- A real repository to practice on (or a sandbox)
Reading map
Section titled “Reading map”L4’s six scenarios are roughly ordered from “low stakes / common” to “high stakes / less common.” New developers should read all six because the high-stakes ones are exactly the scenarios where unprepared developers panic.
Experienced developers can skim the working-directory and staging-area sections; the reflog section is the one to read carefully even if everything else is review.
Managers / TPMs should read the dedicated framing section near the end. The “almost nothing is truly lost” observation is operationally useful for understanding why engineering teams that move fast can do so without losing work.
What this lesson deliberately does not cover
Section titled “What this lesson deliberately does not cover”- Interactive rebase to clean up history before push (deferred to L12)
- Cherry-pick to move commits between branches (deferred to L11)
- Branch-level reset (deferred to L5)
- Pull-request-level undo (deferred to L6)
- Recovering force-pushed remote history (covered in passing; deeper treatment deferred to L12)
If you find yourself wanting to reshape your commit history in cleaner ways, L12 covers interactive rebase. If you need to move a commit from one branch to another, L11 covers cherry-pick.
Estimated reading + practice time
Section titled “Estimated reading + practice time”25 to 30 minutes for reading (longer than L1-L3 because L4 has more distinct commands). 15 to 20 minutes for the practice exercises (deliberately includes a “make a destructive mistake then recover” drill, the muscle memory only develops by doing it once safely).
Total: about 45 to 55 minutes including practice. The practice section’s destructive-recovery drill is worth doing; it transforms the reflog from theoretical knowledge into “I have used this; I know it works.”