Undo an unintended commit in Git

If you work with Git, you may make a commit you didn’t really want to. Perhaps you mistyped the commit message, added a file you didn’t want, didn’t add a file you wanted to, or even just changed your mind about the changes altogether.

There are three useful levels of undoing commits.

First, to just undo the commit itself and leave the files staged for the commit (for example if you mistyped the commit message):

git reset --soft HEAD^

Second, to undo the commit and unstage the changes:

git reset HEAD^

Finally, to undo the commit, unstage the changes AND revert files to their state at the time of the last commit:

git reset --hard HEAD^

For more information, see this answer on Stack Overflow where I found this. Thanks to the people there.

Tags: