How to undo a git merge that’s been pushed

Merged in a new branch, pushed the merge and turns out it’s broken? Don’t panic!

Since it’s already pushed, you probably don’t want to use a git reset if there is any chance that another developer could have pulled in this merge. It will cause all sorts of havoc later if someone else pulled in this merge and then you erase it from history. Not unlike going back in time and killing your grandfather.

It’s quite easy to revert the merge, which keeps history nice and tame.

Checkout onto master if you are not there already (or whatever the parent branch you want to revert from is called).

First, identify the commit that was the merge you want to revert with

$ git log

to list recent commits.

Second, revert the merge with a command that looks like this: (the commit SHA-1 that you want to revert is the long number that looks something like this made up one below)

$ git commit -m 1 00271b6535a6dd483c5e820f13005443f9a70920

The -m 1 message tells git to go back to first commit before the merge happened on the master branch (technically, the ‘parent’ branch, so whatever branch you want to get the merge out of).

It will have the effect of leaving this branch like you never did the merge by creating a commit that undoes everything your merge commit does. Then anyone who pulls your merge can also pull your revert and can likewise recover (unlike with a reset).

Lastly, push your revert like a normal commit.

P.S. reverted the wrong thing? It happened to me today actually. Just revert the revert following the same steps, no worries!