git undo commit

There are a couple of ways to “undo” a git commit.

First

This will create a new commit which reverts the commit identified by the sha. You’ll now have 2 commits in your git history – one being the original commit and another which undoes that commit.

So what do you do if you want the commit totally gone?

Here you can do a combination of 2 commands.

Assuming you want to undo the tip of your branch:

This will unstage the most recent commit. You will be able to see your changes again using git diff.

Now, if you want to get rid of the changes completely, you just do git checkout on all the files in the diff.

Finally, you could just do the above in one step using

I avoid reset –hard because you can’t undo it. I prefer the two step method because I can check my work before I blow things away.