# Finding the name of the parent branch?

11 messages from 2009-01-30 to 2009-01-30. Participants: Pascal Obry, Santi Béjar, Michael J Gruber, Thomas Koch.
Thread: https://gitlist.dev/t/17454

## Pascal Obry, 2009-01-30 10:56

Subject: Finding the name of the parent branch?
Message-ID: <a2633edd0901300256k2ad7530elcaca5cdc30c17534@mail.gmail.com>
URL: https://gitlist.dev/e/a2633edd0901300256k2ad7530elcaca5cdc30c17534%40mail.gmail.com

```
I want to find the name of the parent branch in a script. What is the best way?

                         o---o---o---C
                         /
                    o---o---o---B
                   /
           ---o---o---o---o---o---A

For B I want to get A and for C I want to get B.

All this can be found in gitk, but from a script I did not found a
good solution.

Any idea?

Thanks.
Pascal.

-- 

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|              http://www.obry.net
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595

```

## Santi Béjar, 2009-01-30 11:18

Subject: Re: Finding the name of the parent branch?
Message-ID: <adf1fd3d0901300318s5a0e4c94gab5f31342643ea52@mail.gmail.com>
URL: https://gitlist.dev/e/adf1fd3d0901300318s5a0e4c94gab5f31342643ea52%40mail.gmail.com
In-Reply-To: <a2633edd0901300256k2ad7530elcaca5cdc30c17534@mail.gmail.com>

```
2009/1/30 Pascal Obry <pascal@obry.net>:
> I want to find the name of the parent branch in a script. What is the best way?
>
>                         o---o---o---C
>                         /
>                    o---o---o---B
>                   /
>           ---o---o---o---o---o---A
>
> For B I want to get A and for C I want to get B.

I think your definition is not well defined. A, B and C are just
branches of you project, technically they are equivalent. Maybe you
are thinking that the common commits of, say A and B, really belongs
to A, but this is not the case they belong to both branches. In git a
branch is really just a pointer to a commit and by extension the
history, it is not a series of commits.

Just a counterexample, just rearranging you graph:

                        o---B
                        /
                   o---o---o---o---o---C
                  /
          ---o---o---o---o---o---A

>From you description: For B I would get C and for C I would get A.

HTH,
Santi

```

## Pascal Obry, 2009-01-30 12:56

Subject: Re: Finding the name of the parent branch?
Message-ID: <a2633edd0901300456y48e8d78fn199675f2ae105c8@mail.gmail.com>
URL: https://gitlist.dev/e/a2633edd0901300456y48e8d78fn199675f2ae105c8%40mail.gmail.com
In-Reply-To: <adf1fd3d0901300318s5a0e4c94gab5f31342643ea52@mail.gmail.com>

```
Santi,

Thanks for you reply.

> I think your definition is not well defined. A, B and C are just
> branches of you project, technically they are equivalent. Maybe you

Right. Yet I want to know from which branch a branch as been started.

You need this to get the proper merge-base for example:

   $ git merge-base C A
      1

   $ git merge-base B C
      2

   $ git merge-base B A
      1

I always know on which topic branch I'm but, as shown above, depending on the
parent branch passed to merge-base you do not get the same branch-point. This
is fine.

So, when I'm in a topic branch I want to find the name of the parent
branch. The one given
when creating the branch:

   $ git branch B C

A "stupid" solution whould be to iterate over all branches. Looking
for the merge-base and
at the end output the branch having the youngest merge-base. I'm
looking for something
more efficient...

> are thinking that the common commits of, say A and B, really belongs
> to A, but this is not the case they belong to both branches. In git a
> branch is really just a pointer to a commit and by extension the
> history, it is not a series of commits.
>
> Just a counterexample, just rearranging you graph:
>
>                        o---B
>                        /
>                   o---2---o---o---o---C
>                  /
>          ---o---1---o---o---o---A
>
> From you description: For B I would get C and for C I would get A.

Don't see this as a counter-example as it is exactly my example.

Did I missed something?

Pascal.

-- 

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|              http://www.obry.net
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595

```

## Santi Béjar, 2009-01-30 13:16

Subject: Re: Finding the name of the parent branch?
Message-ID: <adf1fd3d0901300516y3d1bf58dmda9c5172586d828@mail.gmail.com>
URL: https://gitlist.dev/e/adf1fd3d0901300516y3d1bf58dmda9c5172586d828%40mail.gmail.com
In-Reply-To: <a2633edd0901300456y48e8d78fn199675f2ae105c8@mail.gmail.com>

```
2009/1/30 Pascal Obry <pascal@obry.net>:
> Santi,
>
> Thanks for you reply.
>
>> I think your definition is not well defined. A, B and C are just
>> branches of you project, technically they are equivalent. Maybe you
>
> Right. Yet I want to know from which branch a branch as been started.

You can set it when you create the branch:

git branch --track newbranch startpointbranch

(maybe --track is the default these days for remote branches)

And then the config keys:

branch.newbranch.remote
branch.newbranch.merge

will tell you from which branch a branch was started. And it is used
in "git pull" to merge from the tracking branch.

> You need this to get the proper merge-base for example:
>
>   $ git merge-base C A
>      1
>
>   $ git merge-base B C
>      2
>
>   $ git merge-base B A
>      1

1 and 2 are defined in the graph below...

> I always know on which topic branch I'm but, as shown above, depending on the
> parent branch passed to merge-base you do not get the same branch-point. This
> is fine.
>
> So, when I'm in a topic branch I want to find the name of the parent
> branch. The one given
> when creating the branch:
>
>   $ git branch B C

See above.

>
> A "stupid" solution whould be to iterate over all branches. Looking
> for the merge-base and
> at the end output the branch having the youngest merge-base. I'm
> looking for something
> more efficient...
>

Maybe if you explain why you want it (a use case) instead of just this
specific problem...

>> are thinking that the common commits of, say A and B, really belongs
>> to A, but this is not the case they belong to both branches. In git a
>> branch is really just a pointer to a commit and by extension the
>> history, it is not a series of commits.
>>
>> Just a counterexample, just rearranging you graph:
>>
>>                        o---B
>>                        /
>>                   o---2---o---o---o---C
>>                  /
>>          ---o---1---o---o---o---A
>>
>> From you description: For B I would get C and for C I would get A.

Please, if you quote text do not edit it (the 1 and the 2 in this case).

> Don't see this as a counter-example as it is exactly my example.
>
> Did I missed something?

Yes. Compare your sentence and mine:

For B I want to get A and for C I want to get B.
For B I would get C and for C I would get A.

So for B you get A while I get C, and the equivalent for C.

Santi

```

## Pascal Obry, 2009-01-30 13:35

Subject: Re: Finding the name of the parent branch?
Message-ID: <a2633edd0901300535mf2d9ceemd9e9009e0aa18b40@mail.gmail.com>
URL: https://gitlist.dev/e/a2633edd0901300535mf2d9ceemd9e9009e0aa18b40%40mail.gmail.com
In-Reply-To: <adf1fd3d0901300516y3d1bf58dmda9c5172586d828@mail.gmail.com>

```
Santi,

> Maybe if you explain why you want it (a use case) instead of just this
> specific problem...

To know the proper merge base to display all commits done on a specific
topic branch.

>>> Just a counterexample, just rearranging you graph:
>>>
>>>                        o---B
>>>                        /
>>>                   o---2---o---o---o---C
>>>                  /
>>>          ---o---1---o---o---o---A
>>>
>>> From you description: For B I would get C and for C I would get A.
>
> Please, if you quote text do not edit it (the 1 and the 2 in this case).

Well I've just added 1 and 2, nothing changed in the semantic!

> Yes. Compare your sentence and mine:
>
> For B I want to get A and for C I want to get B.
> For B I would get C and for C I would get A.
>
> So for B you get A while I get C, and the equivalent for C.

Ok, that's expected since you have renamed B to C and C to B.

My tree was:


                        o---o---o---C
                        /
                   o---o---o---B
                  /
          ---o---o---o---o---o---A

Your's was:

                       o---B
                       /
                  o---o---o---o---o---C
                 /
         ---o---o---o---o---o---A

So when I said:

    For B I want to get A and for C I want to get B.

It is equivalent to your (just rename B and C).:

   For B I would get C and for C I would get A.

Frankly I do not see your point... That's maybe the cause of the
problem I'm having....

Pascal.

-- 

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|              http://www.obry.net
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595

```

## Michael J Gruber, 2009-01-30 13:35

Subject: Re: Finding the name of the parent branch?
Message-ID: <4983023D.40907@drmicha.warpmail.net>
URL: https://gitlist.dev/e/4983023D.40907%40drmicha.warpmail.net
In-Reply-To: <a2633edd0901300456y48e8d78fn199675f2ae105c8@mail.gmail.com>

```
Pascal Obry venit, vidit, dixit 30.01.2009 13:56:
> Santi,
> 
> Thanks for you reply.
> 
>> I think your definition is not well defined. A, B and C are just
>> branches of you project, technically they are equivalent. Maybe you
> 
> Right. Yet I want to know from which branch a branch as been started.
> 
> You need this to get the proper merge-base for example:
> 
>    $ git merge-base C A
>       1
> 
>    $ git merge-base B C
>       2
> 
>    $ git merge-base B A
>       1
> 
> I always know on which topic branch I'm but, as shown above, depending on the
> parent branch passed to merge-base you do not get the same branch-point. This
> is fine.
> 
> So, when I'm in a topic branch I want to find the name of the parent
> branch. The one given
> when creating the branch:
> 
>    $ git branch B C
> 
> A "stupid" solution whould be to iterate over all branches. Looking
> for the merge-base and
> at the end output the branch having the youngest merge-base. I'm
> looking for something
> more efficient...
> 
>> are thinking that the common commits of, say A and B, really belongs
>> to A, but this is not the case they belong to both branches. In git a
>> branch is really just a pointer to a commit and by extension the
>> history, it is not a series of commits.
>>
>> Just a counterexample, just rearranging you graph:
>>
>>                        o---B
>>                        /
>>                   o---2---o---o---o---C
>>                  /
>>          ---o---1---o---o---o---A
>>
>> From you description: For B I would get C and for C I would get A.
> 
> Don't see this as a counter-example as it is exactly my example.
> 
> Did I missed something?

I think you still haven't *defined* what you mean by "parent branch".
Your example alone doesn't define it, and whenever you have merge
commits things are not clear:

     o---o---o---A
    /     \
---o       o---o---C
    \     /
     o---o---o---B

Now, which one is the "parent branch" of C? A+B? Similarly:

     o---A
    /
---o---C
    \
     o---B

Here it's clear which commit you want, but which branch does it belong
to? A or B?

I really think this is impossible to define unambiguously in git, due to
the nature of git branches, being movable tags, much different from say
hg's hardwired branches (embedded in the commit object).

What you see in gitk is which branch contains a certain commit ("git
branch --contains").

What you want may be the branch which differs from C by the least number
of commits.

Cheers,
Michael

```

## Santi Béjar, 2009-01-30 13:57

Subject: Re: Finding the name of the parent branch?
Message-ID: <adf1fd3d0901300557t19ca4bccn7fe24013a05d2d57@mail.gmail.com>
URL: https://gitlist.dev/e/adf1fd3d0901300557t19ca4bccn7fe24013a05d2d57%40mail.gmail.com
In-Reply-To: <a2633edd0901300535mf2d9ceemd9e9009e0aa18b40@mail.gmail.com>

```
2009/1/30 Pascal Obry <pascal@obry.net>:
> Santi,
>
>> Maybe if you explain why you want it (a use case) instead of just this
>> specific problem...
>
> To know the proper merge base to display all commits done on a specific
> topic branch.
>

gitk topicbranch ^trackingbranch

But I agree that a way to refer to the tracking branch would be great, as:

branch^{origin}

so you can say, for example:

gitk topicbranch ^topicbranch^{origin}

>>>> Just a counterexample, just rearranging you graph:
>>>>
>>>>                        o---B
>>>>                        /
>>>>                   o---2---o---o---o---C
>>>>                  /
>>>>          ---o---1---o---o---o---A
>>>>
>>>> From you description: For B I would get C and for C I would get A.
>>
>> Please, if you quote text do not edit it (the 1 and the 2 in this case).
>
> Well I've just added 1 and 2, nothing changed in the semantic!
>
>> Yes. Compare your sentence and mine:
>>
>> For B I want to get A and for C I want to get B.
>> For B I would get C and for C I would get A.
>>
>> So for B you get A while I get C, and the equivalent for C.
>
> Ok, that's expected since you have renamed B to C and C to B.

I did not rename B to C and C to B, I just draw them differently.

Let's put name to the commits:

Your tree:


                        i---j---k---C
                        /
                   f---g---h---B
                  /
          ---a---b---c---d---e---A

My tree:

                       h---B
                       /
                  f---g---i---j---k---C
                 /
         ---a---b---c---d---e---A

So the commits in all the branches are equal, the only change is that
I painted B above C.

> So when I said:
>
>    For B I want to get A and for C I want to get B.
>
> It is equivalent to your (just rename B and C).:
>
>   For B I would get C and for C I would get A.
>
> Frankly I do not see your point... That's maybe the cause of the
> problem I'm having....

At least part of. You have to understand the branch model:

git model:
  * a branch is just a pointer to a commit
  * you cannot say "this commit was done in that branch"
  * what you can say is "this commit is contained in that branch"

in contrast to other models:
  * where a commit really belongs to a branch (it is specified at
commit time somehow)
  * you can say "this commit was done in that branch"

HTH,
Santi

```

## Pascal Obry, 2009-01-30 14:06

Subject: Re: Finding the name of the parent branch?
Message-ID: <a2633edd0901300606ub4c507bocccb6747b436f01f@mail.gmail.com>
URL: https://gitlist.dev/e/a2633edd0901300606ub4c507bocccb6747b436f01f%40mail.gmail.com
In-Reply-To: <adf1fd3d0901300557t19ca4bccn7fe24013a05d2d57@mail.gmail.com>

```
Santi,

Thanks for your patience!

> gitk topicbranch ^trackingbranch
>
> But I agree that a way to refer to the tracking branch would be great, as:
>
> branch^{origin}
>
> so you can say, for example:
>
> gitk topicbranch ^topicbranch^{origin}

That's exactly what I'm looking for!

> At least part of. You have to understand the branch model:
>
> git model:
>  * a branch is just a pointer to a commit
>  * you cannot say "this commit was done in that branch"
>  * what you can say is "this commit is contained in that branch"

The second point wasn't clear to me.

I had 2 out of 3 OK :)

Thanks.

-- 

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|              http://www.obry.net
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595

```

## Thomas Koch, 2009-01-30 14:26

Subject: Re: Finding the name of the parent branch?
Message-ID: <200901301526.24566.thomas.koch@ymc.ch>
URL: https://gitlist.dev/e/200901301526.24566.thomas.koch%40ymc.ch
In-Reply-To: <a2633edd0901300256k2ad7530elcaca5cdc30c17534@mail.gmail.com>

```
Am Friday 30 January 2009 11:56:12 schrieb Pascal Obry:
> I want to find the name of the parent branch in a script. What is the best
> way?
>
>                          o---o---o---C
>                          /
>                     o---o---o---B
>                    /
>            ---o---o---o---o---o---A
>
> For B I want to get A and for C I want to get B.
>
> All this can be found in gitk, but from a script I did not found a
> good solution.
>
> Any idea?
>
> Thanks.
> Pascal.

Isn't your problem solved by topgit?

-- 
Thomas Koch, YMC AG, http://www.ymc.ch
Phone: +41 (0)71 / 508 24 86

```

## Santi Béjar, 2009-01-30 14:46

Subject: Re: Finding the name of the parent branch?
Message-ID: <adf1fd3d0901300646h39e27f50pdb6510083284cf8a@mail.gmail.com>
URL: https://gitlist.dev/e/adf1fd3d0901300646h39e27f50pdb6510083284cf8a%40mail.gmail.com
In-Reply-To: <a2633edd0901300606ub4c507bocccb6747b436f01f@mail.gmail.com>

```
2009/1/30 Pascal Obry <pascal@obry.net>:
>> At least part of. You have to understand the branch model:
>>
>> git model:
>>  * a branch is just a pointer to a commit
>>  * you cannot say "this commit was done in that branch"
>>  * what you can say is "this commit is contained in that branch"
>
> The second point wasn't clear to me.

A practical example:

$ git clone path/to/project.git
$ cd project
$ git checkout -b bugfix-1234 origin/master
$ # hack, hack, hack
$ git commit -a -m "Fix for the Bug #1234"
$ git push origin HEAD:master

So, you've done a commit in a local branch named bugfix-1234 and once
you push it to the master branch in origin there is nothing to tell
you so. A commit is defined with the current state, the old commit(s)
and some metadata (author and committer) but nothing about which
branch it was made, and as a branch is a pointer to a commit there is
nothing more.

Santi

```

## Pascal Obry, 2009-01-30 15:58

Subject: Re: Finding the name of the parent branch?
Message-ID: <498323AF.5060307@obry.net>
URL: https://gitlist.dev/e/498323AF.5060307%40obry.net
In-Reply-To: <200901301526.24566.thomas.koch@ymc.ch>

```
Thomas Koch a écrit :
> Isn't your problem solved by topgit?

Don't know! I have never used topgit. Why do you think so?

-- 

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|              http://www.obry.net
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595

```
