How to Undo or Revert the Last Git Commit

Undo the last git commit with git reset (keeping or discarding changes) or git revert (creating a new commit that undoes it) — learn when to use each.

Published September 19, 2026

To undo the last commit, use git reset --soft HEAD~1 to keep your changes staged, git reset --hard HEAD~1 to throw them away, or git revert HEAD to add a new commit that cancels it (the safe choice for shared branches).

The concept

reset moves the branch pointer backwards, rewriting history. The flags decide what happens to your files: --soft keeps changes staged, --mixed (default) keeps them unstaged, --hard deletes them.

revert never rewrites history. It creates a new commit that applies the opposite changes, so it is the right tool once the commit has been pushed and others may have it.

Three ways to undo

git reset --soft HEAD~1
commit gone, changes still staged
git reset --hard HEAD~1
commit AND changes gone
git revert HEAD
[main 4f2a1b] Revert "Add login"

Practical examples

Fix the commit message only

git commit --amend -m "Better message"

Undo but keep the work

git reset --soft HEAD~1
# edit files, then commit again

Undo a pushed commit safely

git revert HEAD
git push

Recover after a mistaken reset --hard

git reflog
git reset --hard HEAD@{1}

Common mistakes

  • Using reset --hard on a pushed branch and forcing a push that breaks teammates.
  • Not knowing git reflog can bring back 'lost' commits.
  • Using reset when revert is needed for shared history.

Key takeaways

  • Unpushed: reset. Pushed: revert.
  • --soft keeps changes, --hard discards them.
  • reflog is your safety net.

FAQ

How do I undo several commits?

Use HEAD~3 to go back three commits, or revert a range with git revert HEAD~3..HEAD.

Can I undo a git reset?

Usually yes, find the old commit in git reflog and reset to it.

More Git articles