Git Undo Changes
Knowing how to undo changes in Git removes the fear of making mistakes. The right command depends on where the change lives: in your working directory, in the staging area, or already in a commit (and whether that commit has been pushed).
Discard Changes in a File
This deletes your uncommitted work permanently, so check git diff first. Older guides use git checkout -- file, which does the same thing.
git restore index.html # throw away unstaged edits
git restore . # throw away all unstaged edits(the file returns to its last committed or staged state)
Unstage a File
The change stays in your working directory; it just will not be in the next commit.
git add secrets.txt
git restore --staged secrets.txt$ git status
Untracked files:
secrets.txtFix the Last Commit with --amend
--amend replaces the last commit with a new one. Only amend commits you have not pushed yet, because it rewrites history.
git commit --amend -m "Add contact form with validation"
# or add a forgotten file to the last commit
git add form.js
git commit --amend --no-edit[main 3f4e5d6] Add contact form with validation Date: Fri Oct 30 14:02:11 2026 +0500 3 files changed, 41 insertions(+)
git revert: Undo a Pushed Commit Safely
revert creates a new commit that does the opposite of an earlier one. History is preserved, so it is safe on shared branches.
git revert 9e1f3b7[main 5a6b7c8] Revert "Add contact form" 2 files changed, 30 deletions(-)
git reset: Move the Branch Back
reset moves your branch pointer to an earlier commit. The mode decides what happens to the changes from the removed commits.
- git reset --soft HEAD~1 — undo the commit, keep the changes staged.
- git reset HEAD~1 (--mixed, the default) — undo the commit, keep the changes unstaged.
- git reset --hard HEAD~1 — undo the commit and delete its changes completely.
- Use reset only on commits that have not been pushed and shared.
Recovering with git reflog
Even after a hard reset, the commits usually still exist for a while. git reflog lists every position HEAD has been at, so you can return to one.
git reflog
git reset --hard HEAD@{1}5a6b7c8 HEAD@{0}: reset: moving to HEAD~1
9e1f3b7 HEAD@{1}: commit: Add contact form
HEAD is now at 9e1f3b7 Add contact formFrequently asked questions
How do I undo the last commit in Git?
If it is not pushed, run git reset --soft HEAD~1 to undo it and keep the changes staged. If it is already pushed, use git revert HEAD to create an undo commit.
What is the difference between git reset and git revert?
reset moves the branch back and removes commits from history, which is risky on shared branches. revert adds a new commit that cancels an old one, keeping history intact.
How do I change the last commit message?
Run git commit --amend -m "New message". If the commit was already pushed, you will need git push --force-with-lease, which should be avoided on shared branches.
Can I recover after git reset --hard?
Committed work usually can be recovered with git reflog. Uncommitted changes that were never staged or committed are lost.