Undoing changes: restore, amend, revert and reset
30 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Every Git mistake sits in one place: your files, the staging area or a commit. Each place has its own undo command. Start with the three everyday ones: restore, unstage and amend.
Where is the mistake?
An edit you regret is in the working folder. A file you staged by mistake is in the staging area. A typo in the last commit is in the repository. Each has one command.
git status prints the right undo command next to every changed or staged file. Read it first.Throw away edits: git restore
git restore brings a file back to its last staged or committed version and throws your edits away. With --staged, it only takes the file out of the staging area and keeps your edits.
Priya deleted the Physics line from marks.txt by mistake. The last commit still has the good file.
git restore marks.txtgit statuscat marks.txtOn branch main nothing to commit, working tree clean Maths: 78 Physics: 91 Chemistry: 69 English: 88 Pass mark: 35
git restore <file> cannot be undone: the edits were never committed, so Git has no copy. It works only on files Git already knows.
Unstage a file: git restore --staged
git add . staged notes.txt, Priya's personal to-do list, by mistake. --staged takes it out of the staging area and keeps the file and its edits.
git add .git restore --staged notes.txtgit status -sM README.md ?? notes.txt
After the command notes.txt is untracked again (??) but still on disk, and the README change stays staged (M ).
Fix the last commit: git commit --amend
git commit --amend replaces the last commit with a corrected one: a new message, or extra staged files. The corrected commit gets a new hash.
Priya committed Add Hindi mraks (id 33d1492): a typo. The commit is only on her laptop, so she replaces it.
git commit --amend -m "Add Hindi marks"git log --oneline -2[main 239c7e9] Add Hindi marks Date: Mon Oct 19 10:24:00 2026 +0530 1 file changed, 1 insertion(+) 239c7e9 (HEAD -> main) Add Hindi marks bc3b38e Add run command to README
The amended commit has a new id, 239c7e9, and 33d1492 is gone. Forgot a file? git add it, then git commit --amend --no-edit. Amend only commits that are not pushed yet.
Optional: revert a shared commit, reset a local one
git revert makes a new commit that does the opposite of an old commit. The old commit stays in the history, so revert is the safe undo for commits other people already have.
Priya pushed Raise pass mark to 50, but the rule is 35, and her team already has the commit. She adds a new commit that undoes it instead of rewriting history.
git revert HEAD --no-editgit log --oneline -3[main cc7dd28] Revert "Raise pass mark to 50" Date: Mon Oct 19 10:36:00 2026 +0530 1 file changed, 1 insertion(+), 1 deletion(-) cc7dd28 (HEAD -> main) Revert "Raise pass mark to 50" e9e1013 Raise pass mark to 50 239c7e9 Add pass mark check
Example 1 · Undo a local commit, keep the changes
Runs in: Your terminal
git reset --soft HEAD~1git status -sgit log --oneline -1M README.md cc7dd28 (HEAD -> main) Revert "Raise pass mark to 50"
What this code does
- The
WIPcommit is gone from the branch: HEAD is back oncc7dd28. - Its README change is still staged (
Min the left column), ready forgit commit -m "Add author line to README".
Example 2 · Warning: git reset --hard deletes uncommitted work
Runs in: Your terminal
git status -sgit reset --hardgit statusM marks.txt HEAD is now at c22e3c4 Add author line to README On branch main nothing to commit, working tree clean
What this code does
- Priya had typed
Hindi: 95intomarks.txtbut not committed it (M). git reset --hardmade every file match the last commit, with no question asked. The line was never committed, so Git cannot bring it back.
The log keeps the bad commit and adds a Revert commit that changes 50 back to 35.
git reset --soft HEAD~1 removes your last local commit but keeps its changes staged. git reset --hard also deletes uncommitted edits with no way back, so run git status first. Use reset only for commits still on your laptop.
Next step
Check your understanding
8 quick questions