Three ways to undo the last commit in git.

Undo last commit and destroy all changes

Use this in case you wish to rewind your HEAD to the previous commit and revert all the changes done in the last commit:

1
$ git reset --hard HEAD~1
  • If you do a git status after that, your branch will be clean and you changes gone. (CAUTION !)
  • You can rewind more than one commit. For example, you can go back 5 commits by using HEAD~5

Undo last commit but keep the files unchanged

Use this if you want to rewind your HEAD to the previous commit but keep your changes, as in the files will be still contain your changes. This is what you will probably want to use in most cases.

1
$ git reset HEAD~1
  • If you do a git status after that, you will see the list of all your changes unstaged.
  • You will have to readd your changes with git add. Then commit again.

Undo the last commit, keep the files unchanged and staged changes

Use this if on top of keeping the files unchanged you also want to keep your commit status.

1
$ git reset --soft HEAD~1

If you do a git status after, you will that your changes are already staged and ready to be committed.