Essentially I want to find the name of the original parent branch of a git branch, without having any names to choose from.
o--o--o--o--o--o--o some branch
\ / \
o--o--o--o--o--o my branch
/
---o--o--- some other (irrelevant) branch
So I want to find the name of some branch
, given my branch
, i.e. the branch where my branch
originated from.
Is this possible? From reading other questions it seems it might not be. A common denominator for the questions I've checked out, such as this one, is that they have a few branches to choose from, e.g. "given master
and develop
, which one did my branch
first branch out from?". That seems trivial, but the issue I'm having is that the branch I'm looking for could theoretically be any of a thousand branches, which makes things more difficult.
Essentially, this is what I meant by "original parent branch", i.e. where my branch started, or more precisely: which branch the parent of the first commit on my branch belongs to.
Thankful for any help.
UPDATE: One way I found of doing almost what I want is by making a Python script which loops through some arbitrary number of commits (I'm using 200 right now) and looks for how many branches contain the commit through git branch --contains
:
import subprocess
res = subprocess.Popen(["git", "rev-parse", "--abbrev-ref", "HEAD"], stdout = subprocess.PIPE)
currBranch = res.communicate()[0].splitlines()
for i in range(0, 200):
res = subprocess.Popen(["git", "branch", "--contains", currBranch[0] + "~" + str(i)], stdout = subprocess.PIPE)
out = res.communicate()[0].splitlines()
if len(out) > 1: # don't print if only current branch is in results
print out
However, this is horribly inefficient and very likely error prone as irrelevant branch
would contain the same commit, as well as any other branch that my branch
was merged into.