How To Revert a Merge Commit

If you merged a PR you didn’t want to, and you need to undo it, here’s how. Note that there are some caveats.

Revert a Merge Commit

git revert -m 1 <commit-sha>

With the -m option, we’re specifying the mainline, which I’m willing to bet for you is the thing you merged into. (develop, or master, probably).

If You Didn’t Use Merge Commits

Because you can easily revert them in a single, clean action, I’m a fan of merge commits.

But if you didn’t use them, you have to do just a normal revert of all the necessary commits. Which I think is more error prone, unless you squashed all of them into one commit (which is also a perfectly fine option).

Here’s the long version of this post.

Wow! You read the whole thing. People who make it this far sometimes want to receive emails when I post something new.

I also have an RSS feed.