Currently I am in a feature branch, but I don't know whether it was created from my develop or release branch.
Can anyone please tell me how I can get the parent branch name from the current git branch.
Given that Git uses a directed acyclic graph and usually a repo only has one root, all your branches point back to one initial commit. So what you actually want is the branch that shares the largest part of its history with your branch.
You cannot just look for a branch whose HEAD is contained in your current branch’s history, as this branches HEAD will most likely have moved since then.
So I recommend you use git merge-base
, which finds the newest common ancestor (aka fork point) of two branches or commits:
$ git merge-base feature develop
12345abc
$ git merge-base feature release
98765fed
This will output the two commits that appear in the history of both branches, respectively. One of the two will be contained in both branches, so you can feed them to merge-base
again to get the commit you want (or rather, the one you don’t want):
git merge-base 12345abc 98765fed
98765fed
So in our example, feature
was derived from develop
, as the two share a commit that release
does not have.
Note that this will only work if you don’t do criss-cross-merges between feature and develop.