# How to find the first commit belonging to any branch

3 messages from 2008-07-22 to 2008-07-22. Participants: Kristian Amlie, Junio C Hamano.
Thread: https://gitlist.dev/t/14589

## Kristian Amlie, 2008-07-22 09:08

Subject: How to find the first commit belonging to any branch
Message-ID: <4885A39F.5080209@trolltech.com>
URL: https://gitlist.dev/e/4885A39F.5080209%40trolltech.com

```
Hi all!

I have a question about git: I have one commit sha1, and I would like to 
know the nearest commit that appears in *any* other branch. The sha1 
that I have does not belong to any branch.

The obvious thing to do would be to make a for loop and iterate over 
existing branches while calling git merge-base, but I'm wondering if 
there's a more clever method.

Regards
Kristian Amlie

```

## Junio C Hamano, 2008-07-22 09:27

Subject: Re: How to find the first commit belonging to any branch
Message-ID: <7vej5mmfp9.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vej5mmfp9.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <4885A39F.5080209@trolltech.com>

```
Kristian Amlie <kristian.amlie@trolltech.com> writes:

> I have a question about git: I have one commit sha1, and I would like
> to know the nearest commit that appears in *any* other branch. The
> sha1 that I have does not belong to any branch.
>
> The obvious thing to do would be to make a for loop and iterate over
> existing branches while calling git merge-base, but I'm wondering if
> there's a more clever method.

If the $commit does not belong to any branch, then:

    $ git rev-list --bounardy $commit^0 --not --branches | sed -ne 's/^-//p'

would give you boundary commits of the above traversal, which says:

    Traverse from $commit following the parents, but stop at anything that
    is reachable from any breanch.

which means that the ones that are output are the candidates that are on
some branch.

So pipe that to name-rev like this, perhaps (untested)?

    $ git rev-list --bounardy $commit^0 --not --branches |
      sed -ne 's/^-//p' |
      git name-rev --stdin

```

## Kristian Amlie, 2008-07-22 11:18

Subject: Re: How to find the first commit belonging to any branch
Message-ID: <4885C1F1.7060907@trolltech.com>
URL: https://gitlist.dev/e/4885C1F1.7060907%40trolltech.com
In-Reply-To: <7vej5mmfp9.fsf@gitster.siamese.dyndns.org>

```
Junio C Hamano wrote:
> Kristian Amlie <kristian.amlie@trolltech.com> writes:
> 
>> I have a question about git: I have one commit sha1, and I would like
>> to know the nearest commit that appears in *any* other branch. The
>> sha1 that I have does not belong to any branch.
>>
>> The obvious thing to do would be to make a for loop and iterate over
>> existing branches while calling git merge-base, but I'm wondering if
>> there's a more clever method.
> 
> If the $commit does not belong to any branch, then:
> 
>     $ git rev-list --bounardy $commit^0 --not --branches | sed -ne 's/^-//p'
> 
> would give you boundary commits of the above traversal, which says:
> 
>     Traverse from $commit following the parents, but stop at anything that
>     is reachable from any breanch.
> 
> which means that the ones that are output are the candidates that are on
> some branch.
> 
> So pipe that to name-rev like this, perhaps (untested)?
> 
>     $ git rev-list --bounardy $commit^0 --not --branches |
>       sed -ne 's/^-//p' |
>       git name-rev --stdin
> 
> 

Thanks! That did the trick!

Kristian

```
