Possible Duplicate:
Find the parent branch of a branch
How do I find out the name of the git branch that the branch in question split off from (if any)?
It is easy for us to think master
is always master
and my_branch
is always my_branch
, but that is not the case. Say you have your repository in Github, your windows, your linux and your office.
You therefore have 8 different branches:
github/master
github/my_branch
windows/master
windows/my_branch
linux/master
linux/my_branch
office/master
office/my_branch
You as a human see them as master
and my_branch
but git sees them as 8 different branches. So if you have a network like this:
------------------------------------------------ linux/master
\--/ \-------/ / \-------- office/my_branch
| \---|--\--------/-------------------\------- my_branch
| |
| office/master
|
windows/master
what does it mean to ask where my_branch
comes from? It is a result of merge of many branches!
There, so what I wanted to tell you is that there is a philosophical problem with your question. However there is a way to answer it, although not perfectly. First let's look at git log
:
git log my_branch --pretty=oneline --graph
gives you a nice presentation of merges and stuff. From the git-log man page:
--first-parent
Follow only the first parent commit upon seeing a merge commit. This option can give a better overview when viewing the evolution of a particular topic branch,
because merges into a topic branch tend to be only about adjusting to updated upstream from time to time, and this option allows you to ignore the individual
commits brought in to your history by such a merge.
Using that, you get a linear history of the branch. Removing the graph and outputting only SHA1s, you will get:
git log my_branch --pretty=format:"%H" --first-parent
Using the following command, you can tell which branches contain an SHA1:
git branch --contains <commit>
Putting a script together using these commands, you can use the following script that basically finds the latest SHA1 that is contained in another branch other than the one you are interested in. It then outputs that other branch. (Note: I'm not good in bash scripting yet, so this may not be so efficient):
#! /bin/bash
if [ $# -lt 1 ]; then
branch=master
else
branch=$1
fi
sha1s=$(git log $1 --pretty=format:"%H")
res="Doesn't branch from anything"
for i in $sha1s; do
b=$(git branch --contains $i | awk '{ if (NF > 1) print $2; else print $1 }') # use awk to remove * from current branch
other_branch="";
for j in $b; do
if [ $branch != $j ]; then
other_branch=$j
break;
fi
done
if [ -n "$other_branch" ]; then
res=$other_branch
break
fi
done
printf -- '%s\n' "$res"
I said it's not perfect, because of the following situation. Imagine if my_branch
is branched from master
. In fact you then see the graph like this:
/------------ master
------(master)-----
\------------ my_branch
The initial commits are contained in the history of both branches. It is unknown that they originally came from master. Therefore, this script will tell you that my_branch
was branched from master
and at the same time tells you that master
is branched from my_branch
. There is no way to tell which one was the original one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With