0

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.

Community
  • 1
  • 1
Jokab
  • 2,939
  • 1
  • 15
  • 26
  • Based on your example `my branch` branches off of both `some branch` and `irrelevant branch`. Do you mean the earliest possible branching? – Joseph K. Strauss Jul 31 '15 at 03:20
  • Yes exactly, that's 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. – Jokab Jul 31 '15 at 07:58
  • `some branch` isn't necessarily unique. There could be multiple branches whose heads are equidistant from the commit where `my branch` diverged. – chepner Jul 31 '15 at 11:51
  • @chepner What do you mean? Surely `my branch` could only diverge from a specific commit in `some branch`? Or do you mean that the commit that `my branch` diverged from could belong to many different branches? In that case, I would be content with getting a list of all those branches. Is that possible? – Jokab Jul 31 '15 at 11:57
  • In theory, you could get the list of all branches that share a common ancestor with `my branch`, but if there is more than one, that wouldn't tell you anything about which branch was the active branch when `git branch "my branch"` was run. In practice, there isn't going to be an efficient way to do this, because commits don't store any information about their children, only their parents. You can't walk forward from the merge base to see which branches reference it; you can only check all existing branches to see if they can reach the merge base. – chepner Jul 31 '15 at 12:22
  • Finally, there's no guarantee that the branch that *was* active when `git branch "my branch"` ran still exists. – chepner Jul 31 '15 at 12:23

0 Answers0