How to undo the last commit in Git

If you haven't pushed yet, run git reset --soft HEAD~1: the commit disappears and its changes stay staged. If you already pushed it, use git revert <commit> instead, which adds a new commit that undoes it without rewriting shared history.

Step by step

  1. Undo the commit but keep the changes staged:

    $ git reset --soft HEAD~1
  2. Or keep the changes but unstaged:

    $ git reset HEAD~1
  3. Or throw the changes away completely (careful):

    $ git reset --hard HEAD~1
  4. Already pushed? Undo it with a new commit instead:

    $ git revert <commit-id>

HEAD~1 means "one commit before the current one". The undone commit isn't deleted right away: git reflog can still find it.

Try it in the interactive sandbox →Practice “Undo the commit” with a real repository and teammates, right in your browser.

Related guides