# how to undo a git merge?

8 messages from 2008-07-11 to 2008-07-11. Participants: ff, Miklos Vajna, Jakub Narebski, Sverre Rabbelier.
Thread: https://gitlist.dev/t/14410

## ff, 2008-07-11 16:16

Subject: how to undo a git merge?
Message-ID: <fa7d16350807110916x689e316fr6bae01f28e2e1acb@mail.gmail.com>
URL: https://gitlist.dev/e/fa7d16350807110916x689e316fr6bae01f28e2e1acb%40mail.gmail.com

```
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, 2008-07-11 16:19

Subject: Re: how to undo a git merge?
Message-ID: <20080711161923.GE10347@genesis.frugalware.org>
URL: https://gitlist.dev/e/20080711161923.GE10347%40genesis.frugalware.org
In-Reply-To: <fa7d16350807110916x689e316fr6bae01f28e2e1acb@mail.gmail.com>

```
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, 2008-07-11 17:13

Subject: Re: how to undo a git merge?
Message-ID: <m34p6we43y.fsf@localhost.localdomain>
URL: https://gitlist.dev/e/m34p6we43y.fsf%40localhost.localdomain
In-Reply-To: <fa7d16350807110916x689e316fr6bae01f28e2e1acb@mail.gmail.com>

```
ff <ff@member.org> writes:

> 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, 2008-07-11 18:14

Subject: Re: how to undo a git merge?
Message-ID: <20080711181413.GI10347@genesis.frugalware.org>
URL: https://gitlist.dev/e/20080711181413.GI10347%40genesis.frugalware.org
In-Reply-To: <fa7d16350807111107o40c5cbb5xc06c3c56b16b7499@mail.gmail.com>

```
[ 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'.

```

## Jakub Narebski, 2008-07-11 18:36

Subject: Re: how to undo a git merge?
Message-ID: <200807112036.13745.jnareb@gmail.com>
URL: https://gitlist.dev/e/200807112036.13745.jnareb%40gmail.com
In-Reply-To: <fa7d16350807111108y3a8a7c3di19598a56dbbcdc15@mail.gmail.com>

```
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

```

## ff, 2008-07-11 18:46

Subject: Re: how to undo a git merge?
Message-ID: <fa7d16350807111146m18b0ca9q6902c2167bfd3512@mail.gmail.com>
URL: https://gitlist.dev/e/fa7d16350807111146m18b0ca9q6902c2167bfd3512%40mail.gmail.com
In-Reply-To: <20080711181413.GI10347@genesis.frugalware.org>

```
> [ 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:
> [ 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, 2008-07-11 18:53

Subject: Re: how to undo a git merge?
Message-ID: <20080711185332.GJ10347@genesis.frugalware.org>
URL: https://gitlist.dev/e/20080711185332.GJ10347%40genesis.frugalware.org
In-Reply-To: <fa7d16350807111146m18b0ca9q6902c2167bfd3512@mail.gmail.com>

```
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, 2008-07-11 18:55

Subject: Re: how to undo a git merge?
Message-ID: <bd6139dc0807111155x46a9f6acnf2d585f62342f4a6@mail.gmail.com>
URL: https://gitlist.dev/e/bd6139dc0807111155x46a9f6acnf2d585f62342f4a6%40mail.gmail.com
In-Reply-To: <fa7d16350807111146m18b0ca9q6902c2167bfd3512@mail.gmail.com>

```
On Fri, Jul 11, 2008 at 8:46 PM, ff <ff@member.org> wrote:
>> 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

```
