Skip to content
Laviyx Learn

Try

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.

Beginner7 min readUpdated
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.

Where is the mistake?Command
Edited a file, not stagedgit restore <file>
Staged, not committedgit restore --staged <file>
Last commit, not yet sharedgit commit --amend
Any commit, already sharedgit revert <commit>

Discard edits in a file#

git restore puts a file back to its last committed version:

Terminal
echo "Oops, wrong text" >> notes.txt
git restore notes.txt
cat notes.txt
Output
Buy milk
Call the bank

Bring back a deleted file#

The same command recovers a file you deleted, as long as it was committed:

Terminal
rm notes.txt
git restore notes.txt
ls
Output
notes.txt

You can also take a file from an older commit:

Terminal
git restore --source=HEAD~1 notes.txt
cat notes.txt
Output
Buy milk

This changes only your working copy. git restore notes.txt returns to the latest version:

Terminal
git restore notes.txt
git status --short

Fix the last commit#

git commit --amend replaces the last commit. Use it for a typo in the message:

Terminal
git commit -q --amend -m "Add reminder to call the bank"
git log --oneline
Output
8cd369f Add reminder to call the bank
3b1b8d5 Add shopping note

Or for a file you forgot:

Terminal
echo "Reminders for this week" > README.md
git add README.md
git commit -q --amend --no-edit
git show --stat --oneline HEAD
Output
56409fc 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.

Terminal
git log --oneline
Output
1c34cb1 Add wrong line
56409fc Add reminder to call the bank
3b1b8d5 Add shopping note
Terminal
git revert --no-edit HEAD
git log --oneline
cat notes.txt
Output
[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 bank

The 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:

Terminal
echo "temp" > scratch.txt
git clean -n
git clean -f
ls
Output
Would remove scratch.txt
Removing scratch.txt
README.md
notes.txt

What 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
Terminal
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 2
Output
8246570 Revert "Add mistake"
401b62e Add mistake

Key takeaway#

Next: Branches.

Finished this lesson?