The Plain Picture#
Different kinds of "undo" for different situations: rubbing out a pencil mark you haven't photographed yet; taking a photo out of the album before anyone saw it; or, if friends already have copies of the album, adding a new photo that says "the previous photo was a mistake — here's the correction".
The Real Names — pick the right tool#
| Situation | Command | What it does |
|---|---|---|
| Throw away unstaged edits to a file | git restore quiz.py | resets the working file to the last commit. Edits are lost. |
| Unstage a file (keep the edits) | git restore --staged quiz.py | takes it out of the staging area |
| Fix the last commit's message, or add a forgotten file | git commit --amend | replaces the last commit with a new one (new hash) |
| Undo local commits, keep the changes staged | git reset --soft HEAD~1 | moves the branch back one commit |
| Undo local commits, keep the changes unstaged | git reset HEAD~1 (--mixed, the default) | |
| Undo local commits and throw the changes away | git reset --hard HEAD~1 | ⚠️ discards work |
| Undo a commit that's already pushed | git revert <hash> | creates a new commit that reverses it — safe for shared branches |
| Park unfinished work to switch tasks | git stash / git stash pop | saves changes aside and cleans the working directory / brings them back |
| "I lost a commit!" | git reflog | shows everywhere HEAD has been — find the hash and recover it |
(HEAD~1 means "one commit before HEAD"; HEAD~3 means three before.)
Under the Hood#
reset vs revert — the key interview question#
git reset --hard HEAD~1 git revert B
(rewrites history — local only!) (adds history — safe anywhere)
A───B───C main → A───B main A───B───C main → A───B───C───C' main
▲ ▲ ▲
removed branch moved back C' = "undo the changes made in C"resetmoves the branch pointer backwards — commits after it vanish from the branch. Fine for commits only you have. On pushed commits it causes the same trouble as rebasing shared work (Ch 6).revertleaves history alone and adds a new commit with the opposite changes. It's the right way to undo something onmain— for example, a deploy that broke production.
reflog — Git's safety net#
Git almost never truly deletes a commit immediately. git reflog records every position HEAD has had, including commits
"lost" through reset, rebase or a detached HEAD. From the practice repo:
$ git reflog
80b9b6d HEAD@{0}: commit (merge): Merge branch 'bonus'
640a7d3 HEAD@{1}: commit: Double points
7ef9500 HEAD@{2}: checkout: moving from bonus to main
9ffbfe2 HEAD@{3}: commit: Add bonus points
7ef9500 HEAD@{4}: checkout: moving from main to bonusIf you ran git reset --hard and regretted it, find the commit you want in the reflog and bring it back:
git reset --hard 640a7d3 (or git switch -c recovered 640a7d3). Unreachable commits are cleaned up eventually (after
about 30–90 days by default), so recover sooner rather than later.
What reflog can't save: changes that were never committed or stashed. git restore and git reset --hard on
uncommitted work are truly destructive. Commit or stash first.
Check yourself
5 questionsYou committed with a typo in the message and haven't pushed. How do you fix it?
Show answerHide answer
git commit --amend -m "correct message".
A bad commit is already on main and deployed. How do you undo it, and why not reset?
Show answerHide answer
git revert <hash> — it adds a new undoing commit, so shared history isn't rewritten; reset would break everyone who
already has the commit.
What's the difference between reset --soft, --mixed and --hard?
Show answerHide answer
All move the branch back. --soft keeps the changes staged, --mixed keeps them unstaged, --hard throws them away.
You ran git reset --hard HEAD~2 by mistake. How do you get the commits back?
Show answerHide answer
git reflog, find the commit hash from before the reset, and git reset --hard <hash>.
Can reflog recover edits you never committed?
Show answerHide answer
No — only states that were committed (or stashed).