How to revert a merge commit that's already pushed to remote branch?
The -m
option specifies the parent number. This is because a merge commit has more than one parent, and Git does not know automatically which parent was the mainline, and which parent was the branch you want to un-merge.
When you view a merge commit in the output of git log
, you will see its parents listed on the line that begins with Merge
:
commit 8f937c683929b08379097828c8a04350b9b8e183
Merge: 8989ee0 7c6b236
Author: Ben James <[email protected]>
Date: Wed Aug 17 22:49:41 2011 +0100
Merge branch 'gh-pages'
Conflicts:
README
In this situation, git revert 8f937c6 -m 1
will get you the tree as it was in 8989ee0
, and git revert -m 2
will reinstate the tree as it was in 7c6b236
.
To better understand the parent IDs, you can run:
git log 8989ee0
and
git log 7c6b236
Here's a complete example in the hope that it helps someone:
git revert -m 1 <commit-hash>
git push -u origin master
Where <commit-hash>
is the commit hash of the merge that you would like to revert, and as stated in the explanation of this answer, -m 1
indicates that you'd like to revert to the tree of the first parent prior to the merge.
The git revert ...
line essentially commits your changes while the second line makes your changes public by pushing them to the remote branch.
Ben has told you how to revert a merge commit, but it's very important you realize that in doing so
"...declares that you will never want the tree changes brought in by the merge. As a result, later merges will only bring in tree changes introduced by commits that are not ancestors of the previously reverted merge. This may or may not be what you want." (git-merge man page).
An article/mailing list message linked from the man page details the mechanisms and considerations that are involved. Just make sure you understand that if you revert the merge commit, you can't just merge the branch again later and expect the same changes to come back.