git: Why is it so difficult to get the parent branch name?


It is difficult to find the parent-branch in git …

My current solution:

git show-branch -a 2>/dev/null \
| grep '\*' \
| grep -v `git rev-parse --abbrev-ref HEAD` \
| head -n1 \
| perl -ple 's/\[[A-Za-z]+-\d+\][^\]]+$//; s/^.*\[([^~^\]]+).*$/$1/'


Why is it difficult to find the parent-branch in git?

This question is not about "how to solve this?". It is about "why is it not straight forward?"


git doesn’t store anything about branches.

A commit is an object which points to its parent, and you will have a relationship between commits, but a branch is just a name that points to a commit — a shallow reference with no extra data.

In standard git, there is no information stored which keep track of : "branch x was initially created from y, then was merged with branch z on june 16th, then was reset on that day, then …".

You can only do guess work based on the current commit graph, and the current state of other branches.

This guess work leads to commands such as the one you quoted.

You should also note that depending on the way you manage branches in your own repository, some of these commands may work for someone else but not for you.

Answered By – LeGEC

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published