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

Git gives you two very different ways to undo a commit: git reset rewrites history by moving the branch pointer, while git revert creates a brand-new commit that undoes the changes.

git reset --soft HEAD~1   # undo commit, keep changes staged
git reset --hard HEAD~1   # undo commit, discard changes entirely
git revert HEAD           # create a new commit undoing the last one

Steps

  1. If the commit hasn't been pushed yet, use git reset --soft HEAD~1 to undo it while keeping your changes staged for re-committing
  2. Use git reset --hard HEAD~1 only if you're certain you want to discard the changes entirely — this cannot be undone easily
  3. If the commit has already been pushed and others may have pulled it, use git revert HEAD instead — it adds a new commit rather than rewriting history

How it works

git reset moves the current branch pointer to an earlier commit, effectively erasing later commits from that branch's history. git revert instead calculates the inverse of a commit's changes and applies them as a new commit, leaving the original commit intact in history.

Things to watch for

  • Never git reset --hard or force-push a commit that others have already pulled — it rewrites shared history and will cause conflicts for everyone else
  • git revert is the safe choice for any commit already pushed to a shared branch like main

FAQ

Which command should I use on a shared branch?

Always git revert on shared/pushed branches — it preserves history and doesn't disrupt collaborators. Reserve git reset for commits that only exist locally.

More Git articles