Git and GitHub course menu
How to Undo Changes in Git: restore, amend and revert
Discard edits with git restore, unstage files, fix the last commit with git commit --amend, undo a commit safely with git revert, and choose the right undo command.
On this page
Being able to undo mistakes is the main reason to use Git. The right command depends on how far the mistake has travelled.
Discard edits in a file#
git restore puts a file back to its last committed version:
echo "Oops, wrong text" >> notes.txt
git restore notes.txt
cat notes.txtBuy milk
Call the bankBring back a deleted file#
The same command recovers a file you deleted, as long as it was committed:
rm notes.txt
git restore notes.txt
lsnotes.txtYou can also take a file from an older commit:
git restore --source=HEAD~1 notes.txt
cat notes.txtBuy milkThis changes only your working copy. git restore notes.txt returns to the latest version:
git restore notes.txt
git status --shortFix the last commit#
git commit --amend replaces the last commit. Use it for a typo in the message:
git commit -q --amend -m "Add reminder to call the bank"
git log --oneline8cd369f Add reminder to call the bank
3b1b8d5 Add shopping noteOr for a file you forgot:
echo "Reminders for this week" > README.md
git add README.md
git commit -q --amend --no-edit
git show --stat --oneline HEAD56409fc Add reminder to call the bank
README.md | 1 +
notes.txt | 1 +
2 files changed, 2 insertions(+)--no-edit keeps the same message. Amending creates a new commit with a new ID, replacing the old one.
Undo a commit safely with revert#
git revert does not delete anything. It adds a new commit that does the opposite of an older one, so history stays intact. This is the safe way to undo something on a shared branch.
git log --oneline1c34cb1 Add wrong line
56409fc Add reminder to call the bank
3b1b8d5 Add shopping notegit revert --no-edit HEAD
git log --oneline
cat notes.txt[main 55aade2] Revert "Add wrong line"
Date: Thu Nov 5 09:50:00 2026 +0530
1 file changed, 1 deletion(-)
55aade2 Revert "Add wrong line"
1c34cb1 Add wrong line
56409fc Add reminder to call the bank
3b1b8d5 Add shopping note
Buy milk
Call the bankThe bad line is gone, and the history shows both the mistake and the fix. In practice, you give git revert the ID of any commit you want to undo. (Without --no-edit, Git opens your editor to confirm the message.)
Remove untracked files#
git restore handles tracked files. For new untracked files, git clean deletes them. Always preview first with -n:
echo "temp" > scratch.txt
git clean -n
git clean -f
lsWould remove scratch.txt
Removing scratch.txt
README.md
notes.txtWhat about git reset?#
git reset moves a branch back to an older commit, and can delete commits from a branch. It is powerful enough to need its own lesson.
Common mistakes#
Practice#
Practice exercise
Add the line Mistake to notes.txt and commit it. Then undo that commit with git revert so the history keeps both commits, and print the last two commits.
Show solution
echo "Mistake" >> notes.txt
git add notes.txt
git commit -q -m "Add mistake"
git revert --no-edit HEAD > /dev/null
git log --oneline -n 28246570 Revert "Add mistake"
401b62e Add mistakeKey takeaway#
Next: Branches.
Finished this lesson?