Everyone makes mistakes: an edit you regret, the wrong file staged, a typo in a commit message, a commit that broke production. Git can undo almost anything, but the right command depends on where the mistake is (working tree, staging area, a local commit or a pushed commit). This lesson gives you a clear map so you always pick the safe option.
In simple words
- git restore: throws away edits to a file, or takes a file back out of the staging area.
- --amend: fixes the last commit: its message or forgotten files.
- git reset: moves your branch back to an earlier commit. Only for commits you have not shared.
- git revert: makes a new commit that undoes an old one. The safe way to undo something already pushed.
The map
| Mistake | Command | Safe? |
|---|---|---|
| Edited a file, want the last committed version back | git restore file | discards your edits |
| Staged a file by accident | git restore --staged file | yes, keeps edits |
| Typo in the last commit message, or forgot a file | git commit --amend | only if not pushed |
| Undo the last commit, keep the changes | git reset --soft HEAD~1 | only if not pushed |
| Undo the last commit, keep changes unstaged | git reset HEAD~1 | only if not pushed |
| Throw away commits and changes | git reset --hard HEAD~1 | destructive |
| Undo a commit that is already pushed | git revert <commit> | yes, always |
The rule behind it: never rewrite commits that others already have. Locally you can rewrite freely; once pushed, add a new commit that undoes the old one instead.
Set up a small repository to practise on:
git config --global user.name "Leela Kumar"mkdir undo-labcd undo-labgit initprintf 'TAX_RATE = 0.18\n' > pricing.pyprintf '# Shop\n' > README.mdgit add .git commit -m "Add pricing and README"1. Discard edits in the working tree: git restore
printf 'TAX_RATE = 99\n' > pricing.pygit status -sgit restore pricing.pycat pricing.pygit status -s M pricing.pyTAX_RATE = 0.18git restore file replaces the file with its staged (or last committed) version. Your edits are gone for good, because they were never committed, so double-check with git diff first. Restore a whole folder with git restore src/ or everything with git restore ..
You may see the older git checkout -- file in tutorials. It does the same thing; git restore was added in Git 2.23 to make the intent clearer.
2. Unstage a file: git restore --staged
You ran git add . and staged something you did not mean to commit:
printf 'DEBUG=true\n' > debug.envprintf '\nRun with: flask run\n' >> README.mdgit add .git status -sgit restore --staged debug.envgit status -sM README.mdA debug.envM README.md?? debug.envThe file left the staging area but your work is untouched. (The older equivalent is git reset HEAD file.)
3. Fix the last commit: --amend
Commit, then notice a typo in the message and a forgotten file:
git commit -m "Updte README"rm debug.envprintf 'flask==3.1.0\n' > requirements.txtgit add requirements.txtgit commit --amend -m "Update README and add requirements"git log --oneline[main 6b1d0e2] Updte README 1 file changed, 2 insertions(+)[main 9c4a7f3] Update README and add requirements 2 files changed, 3 insertions(+) create mode 100644 requirements.txt9c4a7f3 (HEAD -> main) Update README and add requirements3e2f1a8 Add pricing and README--amend replaces the last commit with a new one (note the new hash) containing the staged changes and the new message. Use git commit --amend --no-edit to add files without changing the message.
Because amend creates a different commit, never amend something you have already pushed to a shared branch.
4. Move a branch back: git reset
git reset <commit> moves the current branch to an earlier commit. Its mode decides what happens to the changes from the commits you "undo":
| Mode | Branch moves | Staging area | Working tree |
|---|---|---|---|
--soft | yes | keeps changes staged | untouched |
--mixed (default) | yes | reset; changes become unstaged | untouched |
--hard | yes | reset | reset: changes deleted |
Try --soft, which is perfect when you committed too early and want to re-do the commit:
printf 'def total(x):\n return x * 1.18\n' >> pricing.pygit commit -am "Add total"git reset --soft HEAD~1git status -sgit log --onelineM pricing.py9c4a7f3 (HEAD -> main) Update README and add requirements3e2f1a8 Add pricing and READMEThe commit is gone from history, but its change is still staged, ready to commit again.
--hard is the dangerous one. It deletes uncommitted work without asking:
git reset --hard HEADcat pricing.pyHEAD is now at 9c4a7f3 Update README and add requirementsTAX_RATE = 0.18The staged total function is gone. If you reset away commits by accident, git reflog can usually bring them back (lesson 15), but uncommitted changes lost with --hard are gone forever.
5. Undo a pushed commit safely: git revert
When a bad commit is already on the shared main branch, others have it. Rewriting history would break their copies. Instead, git revert creates a new commit that applies the opposite change:
printf 'TAX_RATE = 1.8\n' > pricing.pygit commit -am "Change tax rate"git revert HEAD --no-editgit log --oneline -n 3cat pricing.py[main 5a8e2c1] Change tax rate 1 file changed, 1 insertion(+), 1 deletion(-)[main 0d7b3f9] Revert "Change tax rate" 1 file changed, 1 insertion(+), 1 deletion(-)0d7b3f9 (HEAD -> main) Revert "Change tax rate"5a8e2c1 Change tax rate9c4a7f3 Update README and add requirementsTAX_RATE = 0.18History keeps both the mistake and its fix, which is exactly what you want on a shared branch: an honest audit trail and no surprises for teammates. You can revert any commit, not just the last one: git revert 5a8e2c1.
Restoring one file from an older commit
Sometimes you need just one file as it was:
git restore --source=HEAD~1 pricing.pycat pricing.pygit restore pricing.pyTAX_RATE = 1.8--source takes any commit, branch or tag. The result is a normal uncommitted change you can inspect, commit or discard.
Real-world scenario: the hotfix that made it worse
A hotfix merged to main at 16:00 introduced a new bug. The team's rule is "revert first, investigate second". The on-call engineer ran:
git switch main && git pullgit revert --no-edit 7c1e2d4git pushThe pipeline redeployed in minutes, with the revert commit clearly linked to the bad one in history. Later, the original author fixed the bug properly and reverted the revert (git revert <revert-commit>) together with the fix. Nobody had to force-push, and nobody's local copy broke.
Common mistakes
git reset --hardwith uncommitted work you wanted. Rungit statusandgit stashfirst if unsure.- Amending or resetting pushed commits, then force-pushing over teammates' work.
- Using reset to undo on shared branches instead of revert.
- Forgetting that
git restore filecannot be undone for uncommitted edits. - Confusing
revert(new opposite commit) withreset(move the branch).
Practice
- Edit a file and discard the edit with
git restore. - Stage two files, then unstage one with
git restore --staged. - Make a commit with a typo in the message and fix it with
--amend. - Make a commit and undo it with
git reset --soft HEAD~1; check that the change is still staged. - Make a commit and undo it with
git revert, then look atgit log --oneline.
Cheat sheet
git restore file discard working-tree editsgit restore --staged file unstage, keep editsgit restore --source=HEAD~2 file file from an older commitgit commit --amend [-m msg] replace last commit (not pushed!)git commit --amend --no-edit add staged files to last commitgit reset --soft HEAD~1 undo commit, keep stagedgit reset HEAD~1 undo commit, keep as unstaged editsgit reset --hard HEAD~1 undo commit and delete changesgit revert <commit> [--no-edit] new commit that undoes one (safe when pushed)Key takeaways
- Choose the undo by where the mistake is: working tree, staging area, local commit or pushed commit.
git restorehandles files;--stagedunstages without losing edits.--amendandresetrewrite history: fine locally, never on shared branches.reset --soft,--mixedand--harddiffer in what happens to your changes;--harddeletes them.git revertis the safe undo for anything already pushed.