Undoing Mistakes — The Rescue Toolkit
Everyone makes mistakes. The good news: the librarian keeps a rescue toolkit for every one of them. As long as a page was ever committed, nothing is truly lost. This page is your toolkit — pick the right tool for the mistake.
First rule of rescue: run git status before anything else. It always tells you exactly where you are — and that decides which tool you need.
1 · git restore — Un-Modify a Page
You changed a page and decided you don't like it. The librarian still has the original in the notebook.
git restore books.html
git restore .
This permanently discards uncommitted changes. If the change was never committed, the librarian never saw it — there is no backup. Only use this when you truly don't want the work.
2 · git restore --staged — Take Pages Back Off the Desk
You staged a file (git add) by accident, and it's sitting on the librarian's desk waiting to be committed. Take it back before it gets glued in.
git restore --staged books.html
git reset books.html
git restore works on the working copy. git restore --staged (or git reset) works on the desk (staging area). The page itself is never harmed.
3 · git stash — The Work-in-Progress Drawer
Riya is halfway through the search feature. Suddenly the librarian announces an urgent fix must go to main right now. Her half-done work isn't ready to commit — but switching branches would refuse because the notebook is messy.
git stash sweeps her messy pages into a drawer, cleans the desk, and lets her switch.
git stash
git stash pop
Saved working directory and index state WIP on main: 3f9a2c1 ...
git stash list
Stash = "save my mess somewhere safe so I can deal with an emergency, then get it back." Perfect for unfinished work that isn't ready to commit.
4 · git commit --amend — Fix the Last Sticky Note
Kabir committed, then realised he forgot a page. Or his commit message has a typo. Instead of a new commit, he can rewrite the last sticky note.
git commit --amend -m "Add search page (with results filter)"
git add search.js
git commit --amend --no-edit
--amend rewrites the last commit. Fine for a commit you have not pushed yet — never amend a commit your teammates already pulled.
5 · git revert — The Polite "Take That Back"
Simran merged a feature that broke the app. The cleanest fix on a shared branch is not to rewrite history — it's to write a new diary entry that says "undo the previous entry."
git revert 5f8d0e3
[main 2c9a8e1] Revert "Resolve books conflict"
1 file changed, 2 deletions(-)
Revert adds a new commit that reverses an old one. History stays honest, so it is the safe undo on shared branches like main.
6 · git reset --hard — The Strong Eraser
Sometimes you want to completely reset the notebook to a past state, sweeping away everything after it.
git reset --hard 9b2c4d1
reset --hard deletes commits and uncommitted work — this is the one tool with no safety net (unless the commits were already pushed somewhere). Only use it on your own, never-pushed branch. On shared branches, use git revert instead.
There are gentler versions too: git reset --soft (undo commits but keep everything staged) and git reset --mixed (undo commits, keep working copy) — the same idea, less destructive.
7 · git clean — Clear the Desk of Scraps
Scraps like .tmp files that were never added to the notebook clutter your desk.
git clean -n # dry run: just show what WOULD be deleted
git clean -fd # actually delete files and folders
Always run git clean -n first — these files are untracked, so Git has zero record of them. Deleting them is permanent.
Which Tool When?
| I want to… | Command |
|---|---|
| Discard my edits to a file | git restore <file> |
| Un-stage a file | git restore --staged <file> |
| Save messy work for later | git stash / git stash pop |
| Fix my last commit's message | git commit --amend -m "…" |
| Undo a commit safely (shared) | git revert <hash> |
| Jump back and wipe everything (own branch only) | git reset --hard <hash> |
| See what clean would delete | git clean -n |
The Golden Rule
Before you push: use any tool freely — amend, reset, restore.
After you push (or others pulled): only use tools that add history — git revert. Rewriting shared commits makes everyone's notebook disagree.
Now that you know how to fix mistakes, you can practice fearlessly. See Branch Strategies for how teams choose their workflow, or jump to the Glossary for quick definitions.