简体   繁体   中英

Reliable way of finding 'parent' branch from a checked-out Git branch

We have 3 types of branches in our workflow - master, release branches, and hotfix branches. Release branches are made from master, and hotfix branches are made from release branches.

I'd like to know which release branch my hotfix branch originated from ie which branch was the user on when they ran eg git co -b release_0.1.4_hotfix (the answer being release_0.1.4 ). If I can work out the originating branch, I can do a git diff between my branches, and then land my hotfix on the correct release branch. Our CI system knows the hotfix branch name and provides it as a environment variable for a bash script.

The complication is that the git repo has other things tagging & branching the commits, so I need to filter out irrelevant branches.

I have looked at lots of questions, including How to find the nearest parent of a Git branch? and Finding the original parent branch of a git branch without knowing which ones to choose from , and tried various git commands. The closest I have got is to create a custom log outputting the refs, including only remote branches from refs/remotes , and excluding the known hotfix branch. This returns the original release branch that I am after, but also another non-relevant branch.

git log --format=%d --decorate --decorate-refs="refs/remotes" --decorate-refs-exclude="refs/remotes/origin/release_0.1.4_hotfix"

returns:

 (origin/release_0.1.4, origin/Some_other_branch)

I'm not 100% sure where origin/Some_other_branch is coming from. I presume this commit was on another branch at some point. Maybe the commit was cherry-picked in?

Out of the two remaining branches, is there a way to further refine the results? I was wondering about git log reflog-walk or git log reflog-grep .

In this case, I don't think it is possible to reliably tell which branch is the 'parent' using Git.

(I did find a solution to this problem using our code management tool Phabricator. As hinted at in this file , Arcanist records the remote tracking branch of the branch your are pushing under the 'onto' key of the refs array. By setting the upstream branch correctly, I was able to query the Phabricator API, and retrieve the branch name and the 'onto' branch.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM