With SVN it is easy to reverse-merge a commit, but how to do that with Git?
- 1Duplicate of Revert to a previous Git commituser456814– user4568142014-07-16 21:55:43 +00:00Commented Jul 16, 2014 at 21:55
- 1I don't think this is what is meant by the question. A reverse-merge in SVN also reverts the changes from the commit to local changes, so you can edit them and commit again.Dormouse– Dormouse2016-09-26 10:53:46 +00:00Commented Sep 26, 2016 at 10:53
- 1Possible duplicate of How to revert a merge commit that's already pushed to remote branch?Hidden– Hidden2019-11-26 16:44:27 +00:00Commented Nov 26, 2019 at 16:44
6 Answers
To revert a merge commit, you need to use: git revert -m <parent number>. So for example, to revert the recent most merge commit using the parent with number 1 you would use:
git revert -m 1 HEAD To revert a merge commit before the last commit, you would do:
git revert -m 1 HEAD^ Use git show <merge commit SHA1> to see the parents, the numbering is the order they appear e.g. Merge: e4c54b3 4725ad2
git merge documentation: http://schacon.github.com/git/git-merge.html
git merge discussion (confusing but very detailed): http://schacon.github.com/git/howto/revert-a-faulty-merge.txt
7 Comments
git show <merge commit SHA1> to see the parents, the numbering is the order they appear e.g. Merge: e4c54b3 4725ad2git revert -m 1 SHA1 That command worked for me to revert a merge commit that was several merge commits prior to head and had many commits underneath.git help revert and looking at the -m flag says: This option specifies the parent number (starting from 1) of the mainline and allows revert to reverse the change relative to the specified parent. Although be careful b/c it also says: Reverting a merge commit 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.To create a new commit that 'undoes' the changes of a past commit, use:
$ git revert <commit-hash> It's also possible to actually remove a commit from an arbitrary point in the past by rebasing and then resetting, but you really don't want to do that if you have already pushed your commits to another repository (or someone else has pulled from you).
If your previous commit is a merge commit you can run this command
$ git revert -m 1 <commit-hash> See schacon.github.io/git/howto/revert-a-faulty-merge.txt for proper ways to re-merge an un-merged branch
5 Comments
-m <parent number> option to git revert to specify which change to revert. If you want to undo a merge of non-published history, use git reset --hard HEAD^1.-m means a future merge from the un-merged branch will not include the changes from before that merge! See schacon.github.com/git/howto/revert-a-faulty-merge.txt for proper ways to re-merge an un-merged branch.git reset --hard HEAD^ Use the above command to revert merge changes.
git revert -m hash
-m means mainline.
A merge has two parents.
If you merge branch A to branch B, B is parent 1 and A is parent 2.
Basically you specify, I want to revert the commit hash, which changes based on parent B, then you say
$ git revert -m 1 hash.
If you git log, you will see the merge commit as
commit <hash> Merge: <parent 1> <parent 2> Author: Author <[email protected]> Date: Mon Oct 24 21:54:00 2022 +0000 comment message You check what the <parent 1> and <parent 2> are in the log, you will know which parent/base you want to revert this commit to.