Is there a way to find the parent branch(from which branch HEAD is detached) for detached Head ?

Is there a way to find the parent branch for detached head?

commands like “git log -n 1 --pretty=%d HEAD”, “git show-n 1 --pretty=%d HEAD” are failing when multiple branch HEADs pointing to same commit. I want to know does git maintain any parent branch information when HEAD is detached so that we can know from which branch it is detached.

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?

1 Like
git log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
5 Likes

we can use below command it will show the all the commits from current branch after that will show the parent branch and its commits

git log --first-parent
9 Likes

Thanks skirankumar7!

This answer gave me what I wanted, which was to find whether an old branch I was looking at was started from development or master, so that I could update it from from / merge it back to the right branch without thinking much more about the matter.

This was my final command (gitbash) and output, from which I decided that the current branch was “branched from” development, not master.  (there was too much noise without the grep)

$ git log --first-parent | grep “master|development”
Merge pull request #214 in Repo/Project from development to bugfix/ABC-stuff
Merge pull request #207 in Repo/Project from master to development
Merge branch ‘master’ into development
Merge pull request #199 in Repo/Project from master to development
Merge pull request #198 in Repo/Project from hotfix/ABC-18705-things to development
Merge pull request #197 in Repo/Project from feature/ABC-18629-yadda to development
Merge pull request #192 in Repo/Project from feature/ABC-18334-etc to development
Merge pull request #186 in Repo/Project from master to development
Merge branch ‘development’ of ssh://bitbucket.us.com:9999/Repo/Project into development
… etc …

1 Like

git describe --contains --always --all

2 Likes