Skip to main content

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.

Remember

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.

Throw away changes since the last commit (one file)
git restore books.html
Throw away changes in every file
git restore .
Careful

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.

Un-stage a file (it stays in your working copy)
git restore --staged books.html
Same idea with the classic command
git reset books.html
Remember

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.

Put messy work into the drawer
git stash
Bring the work back out of the drawer
git stash pop
Saved working directory and index state WIP on main: 3f9a2c1 ...
See what's in the drawer
git stash list
Remember

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.

Fix the last commit's message
git commit --amend -m "Add search page (with results filter)"
Add a forgotten file to the last commit
git add search.js
git commit --amend --no-edit
Careful

--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."

Undo commit 5f8d0e3 with a brand-new commit
git revert 5f8d0e3
[main 2c9a8e1] Revert "Resolve books conflict"
1 file changed, 2 deletions(-)
Remember

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.

Jump back to a commit, deleting commits + changes after it
git reset --hard 9b2c4d1
Serious warning

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.

Delete untracked files
git clean -n # dry run: just show what WOULD be deleted
git clean -fd # actually delete files and folders
Careful

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 filegit restore <file>
Un-stage a filegit restore --staged <file>
Save messy work for latergit stash / git stash pop
Fix my last commit's messagegit 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 deletegit clean -n

The Golden Rule​

Never rewrite public history

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.