# finding the merge of two or more commits

4 messages from 2009-10-29 to 2009-10-30. Participants: E R, Avery Pennarun, Jeff King, Jakub Narebski.
Thread: https://gitlist.dev/t/21427

## E R, 2009-10-29 21:12

Subject: finding the merge of two or more commits
Message-ID: <3a69fa7c0910291412l439f7f61vd3b55a77cd7e10b5@mail.gmail.com>
URL: https://gitlist.dev/e/3a69fa7c0910291412l439f7f61vd3b55a77cd7e10b5%40mail.gmail.com

```
Hi,

Given two commits c1 and c2, is it possible to ask git if there are
any commits in the repository that were created by either a sequence
of commands like:

git checkout c1
git merge c2

or:

git checkout c2
git merge c1

with any required conflict resolution?

That is, I don't want to merge c1 and c2 myself, but I want to know if
someone else has merged c1 and c2, performed any conflict resolution
and committed the result.

Thanks,
ER

```

## Avery Pennarun, 2009-10-29 21:34

Subject: Re: finding the merge of two or more commits
Message-ID: <32541b130910291434q1b068918x38c5aec543cb1c2a@mail.gmail.com>
URL: https://gitlist.dev/e/32541b130910291434q1b068918x38c5aec543cb1c2a%40mail.gmail.com
In-Reply-To: <3a69fa7c0910291412l439f7f61vd3b55a77cd7e10b5@mail.gmail.com>

```
On Thu, Oct 29, 2009 at 5:12 PM, E R <pc88mxer@gmail.com> wrote:
> That is, I don't want to merge c1 and c2 myself, but I want to know if
> someone else has merged c1 and c2, performed any conflict resolution
> and committed the result.

Try this:

(git branch -a --contains c1; git branch -a --contains c2) | sort | uniq -d

It's not exactly pretty, but it works.

Have fun,

Avery

```

## Jeff King, 2009-10-30 18:04

Subject: Re: finding the merge of two or more commits
Message-ID: <20091030180429.GD18583@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20091030180429.GD18583%40coredump.intra.peff.net
In-Reply-To: <32541b130910291434q1b068918x38c5aec543cb1c2a@mail.gmail.com>

```
On Thu, Oct 29, 2009 at 05:34:45PM -0400, Avery Pennarun wrote:

> On Thu, Oct 29, 2009 at 5:12 PM, E R <pc88mxer@gmail.com> wrote:
> > That is, I don't want to merge c1 and c2 myself, but I want to know if
> > someone else has merged c1 and c2, performed any conflict resolution
> > and committed the result.
> 
> Try this:
> 
> (git branch -a --contains c1; git branch -a --contains c2) | sort | uniq -d
> 
> It's not exactly pretty, but it works.

That will tell you whether any branch contains both of them, which is
not exactly what the original poster asked for (though it may have been
what he meant :) ). To see if there is a specific merge of those two
commits, you can do:

  git log --all --format='%H %P' | grep " $c1" | grep " $c2" | cut -d' ' -f1

-Peff

```

## Jakub Narebski, 2009-10-30 23:38

Subject: Re: finding the merge of two or more commits
Message-ID: <m3iqdwcv17.fsf@localhost.localdomain>
URL: https://gitlist.dev/e/m3iqdwcv17.fsf%40localhost.localdomain
In-Reply-To: <3a69fa7c0910291412l439f7f61vd3b55a77cd7e10b5@mail.gmail.com>

```
E R <pc88mxer@gmail.com> writes:

> Given two commits c1 and c2, is it possible to ask git if there are
> any commits in the repository that were created by either a sequence
> of commands like:
> 
> git checkout c1
> git merge c2
> 
> or:
> 
> git checkout c2
> git merge c1
> 
> with any required conflict resolution?
> 
> That is, I don't want to merge c1 and c2 myself, but I want to know if
> someone else has merged c1 and c2, performed any conflict resolution
> and committed the result.

I assume that commits c1 and c2 are not one ancestor of the other (are
not in fast-forward relation).

Translating your question into question about DAG of revisions, you
want to check if there is branch for which both c1 and c2 are
reachable from:

  c1sha=$(git rev-parse c1)
  c2sha=$(git rev-parse c2)
  git for-each-ref --format="%(refname)" refs/heads/ |
  while read refname
  do 
      b1=$(git merge-base c1 $refname)
      b2=$(git merge-base c2 $refname)
      if [ "$b1" = "$c1sha" -a "$b2" = "$c2sha" ]
      then
          print ${refname#refs/heads/}
      fi
  done

Instead of comparing git-merge-base with SHA-1 of c1 and c2
respoectively, you can count commits:

  git for-each-ref --format="%(refname)" refs/heads/ |
  while read refname
  do 
      count1=$(git rev-list c1..$refname | wc -l)
      count2=$(git rev-list c2..$refname | wc -l)
      if [ "$count1" > 0  -a  "$count2" > 0 ]
      then
          print ${refname#refs/heads/}
      fi
  done

Not tested!

-- 
Jakub Narebski
Poland
ShadeHawk on #git

```
