1

Given a specified branch, how do I find which branch it was branched from?

I know that Git doesn't actually support what most SCM's would call branches, so it's a question that is either difficult to answer, or in some cases perhaps doesn't even have an answer. Nevertheless our branching strategy (GitFlow) is branch heavy and therefore we commonly have a need to examine the 'branch' structure of the repository.

Given what I know of Git's lack of understanding of branches I frankly struggle to know how to even formulate the question in terms that make sense in Git.

I do know that it's possible to determine which branch 'contains' another using git branch --contains <commit> so perhaps one Git-like way to formulate the question would be, "how do I determine the first ancestor commit of a branch that is included in at least one other branch that contains the specifed branch?"

I'm not completely sure that's the right question, but it's the best I can do.

I'm looking for a solution that is robust in the face of the branch having synchronisation merges from its parent into itself, or being merged back into its parent.

I'm also looking for a solution that's at least somewhat user friendly, ideally a single Git command or a TortoiseGit gui approach, rather than a BASH or Python script.

Neutrino
  • 8,496
  • 4
  • 57
  • 83
  • What makes a parent branch a parent branch? Solely the fact that another branch was created from it? In that case I'm not sure there's any information kept on that – NotFound Mar 25 '20 at 10:53
  • In what universe is a gui more user friendly that a bash script? – William Pursell Mar 25 '20 at 10:54
  • 2
    “It ain't what you don't know that gets you into trouble. It's what you know for sure that just ain't so.” You're asking how to find useless information. That's why Git has never bothered tracking it. What actual problem are you planning on solving with this information? – jthill Mar 25 '20 at 10:56
  • If I can identify the parent of a feature branch, then I can use something like TortoiseGit to show a log of the feature branch alongside its parent which will enable me to see how the feature was developed over time and when the feature had updates from its parent merged in. – Neutrino Mar 25 '20 at 11:02
  • @WilliamPursell In the one not entirely populated by elitist command line edge lords. – Neutrino Mar 25 '20 at 11:25
  • See https://stackoverflow.com/q/3161204/1256452 for more about this. – torek Mar 25 '20 at 16:51
  • I'd seen that question already but the only 'solutions' presented are a BASH script or the chained execution of a load of UNIX utilities. I'm on Windows and I really don't want to install a UNIX shell environment just to examine basic state of my Git repo. If I have to go that route I'd rather just write my own command in C#, but I'd be rather peeved to have to put in that much effort just to get basic information about a Git repo. – Neutrino Mar 26 '20 at 11:31

1 Answers1

0

1/3, tl;dr, assuming OP's not leaving out anything important:

git log  develop..feature-x

2/3: If we make some simplifying assumptions, there's an easy answer for what seems to be the real question, I'll get to that.

For the title question, "how do I find the parent branch",

our branching strategy (GitFlow)

in the body makes it easy: two minutes with tfm gives simple, prominently specified, explicit constraints on how branches can split and merge. For master, there's no parent. For develop, the parent is master. For anything beginning hotfix-, the parent is master. For everything else, the parent is develop.

OP's view is so larded with false premises I'm simply going to bypass them with what I sincerely hope is a jarring quip: letting the abstraction monkey ride you like a scared donkey is glorious fun for the monkey, there's hope it's less so for you, and it's much, much less so for anybody nearby who's got business to attend to, though the spectacle does at least afford some amusement for any who can spare the time.

But this, from comments,

show a log of the feature branch alongside its parent which will enable me to see how the feature was developed over time and when the feature had updates from its parent merged in

is I think the question OP's trying to get at. In Git Flow's case every feature branch is branched off develop and merged back to develop once, when it's done, and then deleted. So if a branch let's call feature-x hasn't been deleted, the answer is

git log  develop..feature-x

with whatever detail options you want. If it has been deleted, then either (a) it's been merged into develop, in which case you're looking for

featurexmerge=`git rev-parse "develop^{/^Merge branch 'feature-x'}"`
git log featurexmerge^1..featurexmerge^2

(see the git rev-parse docs) because the mergeback's first parent was develop as of the merge, and its second was feature-x; or (b) it hasn't, the branch was abandoned in its origin repo, in which case the only thing to do is to hunt through the HEAD reflog in that repo for commits made on that branch and run the above for the latest of those.


3/3: ... nah. there is no part 3 until OP fills in a lot of apparently missing details about the true state of affairs in this repo. The completely general case, where no discipline at all has been followed and the history is an arbitrary, unholy mess with nobody around to explain even what the idea was, is solvable with some --first-parent forensics, it's not even hard, but (a) ewww, and (b) that's not what was asked.

jthill
  • 55,082
  • 5
  • 77
  • 137
  • I see only one premise in my original question, specifically "that Git doesn't actually support what most SCM's would call branches". So I'm rather curious as to what are these false premises you allude to? – Neutrino Mar 26 '20 at 11:04
  • Your simplifying assumptions have resulted in you answering a different question to the one I asked. I don't want to _presume_ what the parent branch is based on the assumption that the person creating the feature branch has correctly followed the GitFlow branching strategy. Instead I want to _determine_ for myself from an examination of the repository what the parent branch is. Besides, the branch may not be a feature branch, it might be a work branch for a spike off of a feature branch. – Neutrino Mar 26 '20 at 11:10
  • Lastly `git log develop..feature-x` returns nothing for a branch that's already been merged back into its parent. – Neutrino Mar 26 '20 at 11:11