Undo Changes in Git: restore, reset and revert
Everyone makes mistakes, and Git has an undo for nearly all of them. The trick is choosing the command that matches where the mistake is: in an unsaved edit, in the staging area, or in a commit.
Discard an edit: git restore
git restore notes.txt throws away uncommitted changes to the file and brings back the version from the last commit (strictly, from the staging area). This is one of the few Git commands that can destroy work, since the discarded edit was never saved anywhere. Double-check with git diff first.
Unstage: git restore --staged
git restore --staged notes.txt takes a file out of the staging area but keeps your edits in the working directory. Use it when you ran git add . and caught something that should not be in this commit.
Fix the last commit: --amend
git commit --amend -m "Better message" replaces the most recent commit with a new one that includes anything you have staged and the new message. It is perfect for a typo or a forgotten file, before you have shared the commit.
Move a branch back: git reset
git reset moves the current branch to an earlier commit:
git reset --soft HEAD~1undoes the last commit but keeps its changes staged.git reset HEAD~1(the default,--mixed) keeps the changes in your files, unstaged.git reset --hard HEAD~1discards the commit and the changes. Use with great care.
Because reset rewrites history, use it only on commits that exist nowhere else.
Undo a shared commit: git revert
git revert <commit> creates a new commit that does the opposite of an old one. History is not rewritten, so it is safe on branches other people have pulled. This is the right way to back out a change that is already on a shared main.
git init -q
echo "Important notes" > notes.txt
git add notes.txt
git commit -q -m "Add notes"
echo "oops, overwritten" > notes.txt
git restore notes.txt
cat notes.txt
echo "draft" > draft.txt
git add draft.txt
git status --short
git restore --staged draft.txt
git status --short
echo "Bad idea" >> notes.txt
git commit -q -am "Add bad idea"
git revert --no-edit HEAD
cat notes.txt
git log --oneline --format="%s" Transcript (verified by our test suite in a fresh temp folder)
$ git init -q $ echo "Important notes" > notes.txt $ git add notes.txt $ git commit -q -m "Add notes" $ echo "oops, overwritten" > notes.txt $ git restore notes.txt $ cat notes.txt Important notes $ echo "draft" > draft.txt $ git add draft.txt $ git status --short A draft.txt $ git restore --staged draft.txt $ git status --short ?? draft.txt $ echo "Bad idea" >> notes.txt $ git commit -q -am "Add bad idea" $ git revert --no-edit HEAD [main 74580dd] Revert "Add bad idea" Date: Thu Jan 15 10:00:00 2026 +0000 1 file changed, 1 deletion(-) $ cat notes.txt Important notes $ git log --oneline --format="%s" Revert "Add bad idea" Add bad idea Add notes
The transcript damages a file and restores it, unstages a file, then reverts a commit and shows that history grew by one commit instead of shrinking.
A quick decision guide
| Situation | Command |
|---|---|
| Edited a file, want the committed version back | git restore file |
| Staged something by accident | git restore --staged file |
| Last commit has a typo, not pushed | git commit --amend |
| Last commit was a mistake, not pushed | git reset --soft HEAD~1 |
| A pushed commit must be undone | git revert <hash> |
Summary
restorefor files,restore --stagedfor the staging area.resetand--amendrewrite local history; keep them for unshared commits.revertadds an undo commit and is safe on shared branches.
Quiz: Undoing changes