Skip to contentSkip to conversation

How to find the first commit belonging to any branch

DISCUSSION Kristian Amlie 2 participants 3 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

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

#1 lore raw
Quoted 7 lines · Kristian Amlie

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
#2 lore raw
Quoted 19 lines · Junio C Hamano
Quoted 7 lines · Kristian Amlie

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.

Quoted 10 lines

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