Git: how to revert merge commit


TL;DR: git revert -m 1 <commit>

Let’s say you merged a pull request on GitHub but now need to revert it.

Find merge commit

Given you’re on latest master:

$ git checkout master
$ git pull

To find the merge commit:

$ git log --grep='Merge pull request'

Copy the commit hash, which looks something like this:

ad8a33e72fc9ed5769f96b0bbb1e222e4bebf44c

Revert on master

To revert a merge commit on master (see Stackoverflow answer):

$ git revert -m 1 <commit> # replace `<commit>` with hash

Now you can push your revert to master:

$ git push

Revert on branch

Create a branch for the revert:

$ git checkout -b revertBranch

Revert the merge commit:

$ git revert -m 1 <commit> # replace `<commit>` with hash

Push the branch up and open a pull request:

$ git push origin revertBranch


If you enjoyed this post, please consider supporting this site!