Skip to content

How to undo changes in Git: restore, amend, revert

November 3, 2026 · 2 min read

Pick the undo by how far the mistake has gone: git restore for an edit or a staged file, git commit --amend for your last commit, and git revert for a commit you have already pushed.

git status --short
#  M notes.txt          (edited, not staged)
git restore notes.txt   # edit thrown away

git add notes.txt
git status --short
# M  notes.txt          (staged)
git restore --staged notes.txt   # unstaged, edit kept

git commit -am "Add mroe"
git commit --amend -m "Add more"
git log --oneline
# 45c4723 Add more
# 14d84fc Add notes

git revert --no-edit HEAD
git log --oneline
# 8decab2 Revert "Add more"
# 45c4723 Add more
# 14d84fc Add notes

Your hashes will differ, but the shape of the output will match.

What each command does

  • git restore file puts the file back as it was in the last commit. The edit was never committed, so Git can't bring it back. Run git diff first.
  • git restore --staged file takes the file out of the staging area and keeps your edit. In the short status, the M moves from the first column (staged) to the second (not staged).
  • git commit --amend replaces the last commit with a corrected one. Add --no-edit to keep the message, for example after staging a forgotten file. The hash changes because it is a new commit.
  • git revert hash adds a new commit that does the opposite of an old one. History stays intact, so it is safe on shared branches. Without --no-edit, Git opens your editor for the message.

Common mistake

Amending a commit you have already pushed. The amended commit has a new hash, so your teammates' history no longer matches yours. Don't fix it with git push --force. Make a new commit, or use git revert.

Reaching for git reset --hard. Many online answers suggest it. It deletes uncommitted changes without asking. If you are unsure, run git stash first. If you lose commits, git reflog can usually find them.

Takeaway

Edit or staged file: git restore. Last commit, not pushed: --amend. Anything already shared: git revert.

More on Git & Tools