threads / discuss / 14410

how to undo a git merge?

Subject: how to undo a git merge?

## tl;dr

8 messages between Jul 11, 2008 and Jul 11, 2008.

replies: 7people: 4as markdown or json

ff· Jul 11, 2008, 16:16 UTC · lore
Hello,
I'm a git lover with a question.

I have two git branches that stem from the same exact master branch. Many commits happened independently on these two branches overtime. Then, I inadvertently did a merge from one branch into another. The branch where I merged into now has mixed commits, and also a commit for the merge itself. I then had more commits added to the branch that I merged into.

Now I would like to revert the merge... what is the best way of doing that? I would like very much to use something like git-revert <merge_commit_id> But that does not seem to work.

Any help/pointers of an example illustrating solution to this case would be greatly appreciated!

Regards,
-- ff@member.org
Miklos Vajna· Jul 11, 2008, 16:19 UTC · re: ff · lore

Re: how to undo a git merge?

On Fri, Jul 11, 2008 at 12:16:15PM -0400, ff <ff@member.org> wrote:
> Now I would like to revert the merge... what is the best way of doing that?
> I would like very much to use something like git-revert <merge_commit_id>
> But that does not seem to work.
git revert has a '-m' option to revert merges. See the manpage.
> Any help/pointers of an example illustrating solution to this case
> would be greatly
> appreciated!

Actually if you just did that merge by accident, then probably you want 'git reset --hard HEAD^', not git revert, I think.

Jakub Narebski· Jul 11, 2008, 17:13 UTC · re: ff · lore

Re: how to undo a git merge?

ff <ff@member.org> writes:
Show 9 quoted lines
> I have two git branches that stem from the same exact master branch.
> Many commits happened independently on these two branches overtime.
> Then, I inadvertently did a merge from one branch into another. The branch
> where I merged into now has mixed commits, and also a commit for the merge
> itself. I then had more commits added to the branch that I merged into.
> 
> Now I would like to revert the merge... what is the best way of doing that?
> I would like very much to use something like git-revert <merge_commit_id>
> But that does not seem to work.

If you have published history, then the only sane option is "git revert -m". If you haven't published history, you can try "git rebase --interactive".

-- 
Jakub Narebski
Poland
ShadeHawk on #git
Miklos Vajna· Jul 11, 2008, 18:14 UTC · lore

Re: how to undo a git merge?

[ Did you reply off-list intentionally? ]
On Fri, Jul 11, 2008 at 02:07:44PM -0400, ff <ff@member.org> wrote:
Show 7 quoted lines
> thank you.
> 
> I did see the -m option in the revert man page. It talks about
> "parent" and I did
> not understand what that is. Is parent the commit id of the merge commit?
> 
> Thanks again!
http://www.kernel.org/pub/software/scm/git/docs/gitglossary.html#def_parent

A merge commit has two parents, but obviously only one of the was the HEAD commit before the merge. So when you revert a merge, you need to specify which which parent's tree should be the tree of the new HEAD.

For example, if you were on branch 'master' and you merged 'foo' to master using 'git merge foo', and you want to revert that merge then you need '-m 1'.

ff· Jul 11, 2008, 18:46 UTC · re: Miklos Vajna · lore

Re: how to undo a git merge?

> [ Did you reply off-list intentionally? ]
ooops... yes... I'm scared of the reply-all button. :)
> For example, if you were on branch 'master' and you merged 'foo' to
> master using 'git merge foo', and you want to revert that merge then you
> need '-m 1'.
Ok, so... sorry for not getting it completely, even after reading the info.

In your example, how and why you can determine that the number 1 represents the "foo" branch? Would "-m 2" represent the master branch? In your example is there any other choice for the "-m" number to use?

-- ff
On Fri, Jul 11, 2008 at 2:14 PM, Miklos Vajna <vmiklos@frugalware.org> wrote:
Show 21 quoted lines
> [ Did you reply off-list intentionally? ]
>
> On Fri, Jul 11, 2008 at 02:07:44PM -0400, ff <ff@member.org> wrote:
>> thank you.
>>
>> I did see the -m option in the revert man page. It talks about
>> "parent" and I did
>> not understand what that is. Is parent the commit id of the merge commit?
>>
>> Thanks again!
>
> http://www.kernel.org/pub/software/scm/git/docs/gitglossary.html#def_parent
>
> A merge commit has two parents, but obviously only one of the was the
> HEAD commit before the merge. So when you revert a merge, you need to
> specify which which parent's tree should be the tree of the new HEAD.
>
> For example, if you were on branch 'master' and you merged 'foo' to
> master using 'git merge foo', and you want to revert that merge then you
> need '-m 1'.
>
Miklos Vajna· Jul 11, 2008, 18:53 UTC · re: ff · lore

Re: how to undo a git merge?

On Fri, Jul 11, 2008 at 02:46:15PM -0400, ff <ff@member.org> wrote:
> ooops... yes... I'm scared of the reply-all button. :)
Also please don't top-post, thanks. ;-)
> In your example, how and why you can determine that the number 1
> represents the "foo" branch? Would "-m 2" represent the master branch?
> In your example is there any other choice for the "-m" number to use?

If the merge has two parents, then the first one is the actual branch and the second is the 'other' branch.

So if 'master' is checked out, you did a 'git merge foo', and you want to do a revert, '1' stands for 'master' and '2' for 'foo'.

Sverre Rabbelier· Jul 11, 2008, 18:55 UTC · re: ff · lore

Re: how to undo a git merge?

On Fri, Jul 11, 2008 at 8:46 PM, ff <ff@member.org> wrote:
Show 7 quoted lines
>> For example, if you were on branch 'master' and you merged 'foo' to
>> master using 'git merge foo', and you want to revert that merge then you
>> need '-m 1'.
>
> In your example, how and why you can determine that the number 1
> represents the "foo" branch? Would "-m 2" represent the master branch?
> In your example is there any other choice for the "-m" number to use?

When you perform a merge on branch A, like so: git checkout A # first switch to the branch you want the merge to be on git merge B # now do the actual merge with the branch you want to merge with

The resulting merge commit will have the head of A as it's first parent, and the head of B as it's second parent. With the -m switch you can specify which parent you want to follow. If for example, you have a three-way merge, like so ..: git checkout A git merge B C .. the resulting merge commit would have A as it's first, B as it's second and C as it's third parent. If you want to revert to the state of C then you specify -m 3, so that the third parent is chosen. If you performed the merge on the branch that you want to revert to, you always specify -m 1. If you did this ..: git checkout B git merge A .. and then you want to restore the state of branch A, you use '-m 2'.

-- 
Cheers,

Sverre Rabbelier
Jakub Narebski· Jul 11, 2008, 18:36 UTC · lore

Re: how to undo a git merge?

ff (?) wrote:
> 
> I did see the -m option in the revert man page. It talks about
> "parent" and I did not understand what that is. Is parent the commit
> id of the merge commit? 
It is _number_ of parent.
   -m parent-number, --mainline parent-number
          Usually you cannot revert a merge because you do not know which side
          of  the  merge should be considered the mainline. This option speci-
          fies the parent number (starting from 1) of the mainline and  allows
          revert to reverse the change relative to the specified parent.

Merge commit has more than one parent. What git-revert does, it creates commit which reverts the changes, as if applying "reversal" diff, "git diff -R <revision>^<parent-number> <revision>".

-- 
Jakub Narebski
Poland

← back to recent threads