Frerich Raabe Frerich Raabe - 2 months ago 6
Git Question

How can I see what branch another branch was forked from?

My git repository has three branches,

devel
,
stable
and
customers/acme_patches
. A long time ago,
stable
was forked from
devel
, and all the bugfixing takes place in
stable
. Every now and then,
stable
is merged back into
devel
.
customers/acme_patches
is a branch with a few customer-specific patches. The branch wasn't merged into either of
devel
and
stable
.

A bit of ASCII art to illustrate the scenario:


o---o---o customers/acme_patches?
/
o---o---1---o---o---o stable
/ \ \
o---o---o---2---o---o---o---o devel
\
o---o---o customers/acme_patches?


Now I wonder:

What branch was
customers/acme_patches
forked from -
devel
or
stable
? I only know that it was forked off one of them in the past, but I don't know which. E.g. it might have been commit
1
or
2
in the above diagram.

I've been playing around with
git log --oneline --graph
and
gitk
but since
customers/acme_patches
was forked a few hundred commits ago, it's hard to follow the lines being drawn.

Is there maybe a quick command (a little script is fine, too) which can somehow follow the commits in
customers/acme_patches
backwards to find the first commit with two children (the fork point) and then determines whether that commit was done in
stable
or in
devel
?

In the best case, I could just execute something like (excuse the prompt, I'm on Windows):

C:\src> git fork-origin customers/acme_patches
stable

Answer

Well, there is probably no perfect solution to this answer. I mean there is no fork-origin equivalent in git (to my knowledge). Because the stable branch is merged into devel, your acme_patches (from 1) is on both devel and stable branch.

What you could possibly do is:

git branch --contains $(git merge-base customers/acme_patches devel stable)

If you have stable and not devel, or devel and not stable, then you know where it comes from.

For example, in the case 2, you would have

$ git branch --contains $(git merge-base customers/acme_patches devel stable)
customers/acme_patches
devel

while in case 1 you would have

$ git branch --contains $(git merge-base customers/acme_patches devel stable)
customers/acme_patches
devel
stable

As it's now on both branches (because of the merge from stable to dev)