Git doesn’t track parent branches because branches are “references” to specific commits. It is the commits themselves that track parentage. You can see this when you do git show --pretty=raw
and supply the SHA of an individual commit. Here’s part of one from the atom/atom repository:
commit 9a5cd1eb10d476466ef583378eda3a8844815b53
tree 8bc38bebbdafd15049f7d489d1f32feccd809a64
parent 067add71f8147d3864e3885eadecf0f7052ca299
author simurai <simulus@gmail.com> 1523517404 +0900
committer GitHub <noreply@github.com> 1523517404 +0900
gpgsig -----BEGIN PGP SIGNATURE-----
You can see the parent SHA on the third line. So in order to find the “parent” branch, you walk the chain of commits back up the tree. The problem is that a single commit can have multiple parent commits (anywhere from zero to three, from what I recall). So if a single commit can be backtracked to multiple points where it meets a branch reference, which is the “real parent”? Additionally, a single commit may not meet a branch reference ever. Consider this graph:
What is the parent branch of the detached HEAD named ?
? You’ll probably say master
is. But now what if there are fifteen other branches along that line from A
to Z
? Which is the parent branch then?
Now, it doesn’t have to be this complicated. Because a “detached HEAD” just means that the current HEAD
reference is not referenced directly by any branch reference. In the graph above, if HEAD
was pointing to C
, that would be a “detached HEAD” because master
(the only branch reference) is pointing to Z
, not C
… even though C
is a direct ancestor to Z
.
So given that “parent branch” is a rather fluid concept, what is it you’re trying to figure out?