简体   繁体   中英

Identifying a tag belongs to which branch in git

I first did repo sync to a manifest for a branch name myBranch. I then get the tags from

git tag -l

Now I want to know using git that each tag obtained as a result of git tag -l was actually created on which branch. Please note: I do not want myBranch as the output but the remote branch name on which the tag was created.

Keeping aside the fact that branches can be renamed or deleted at any time (without losing any commits, which can still be referenced in the path of another branch), the best you can do is:

  • get the branches that contains the commit referenced by the tag

See " Show the original branch for a commit ", combined with " Git - how to tell which commit a tag points to ".
(Ie, a combination of git rev-parse <tag>~0 with git branch --contains <sha1> )

This has nothing to do with the branch on which the tag was created , but rather the branch(es) which currently reference said tag.

No such thing. Tags point to commits, and branches point to commits. A single commit can be pointed at (or be a parent of) dozens of different branches; there is no way to narrow down one specific branch as "the owner of this tag". The branch might have been deleted from upstream before you fetched it, and only the commit remains, as another example of why this can't work.

I was able to solve this in two ways


Using the solution given by @VonC I was able to identify for the available tags its source branch

for tag in $(git tag)
do
    commit=$(git rev-parse $tag~0)
    echo "$tag: $(git rev-parse $commit~0) | branch: $(git branch -r --contains $commit)" | grep branch
done

the output will look like

qa-test-integrated-v0.0.3d: 382d0553d189a8a1591667a3806ac98d0f11394a | branch:   origin/HEAD -> origin/master
qa-test-integrated-v0.0.3e: 1a5329d611fe15db6704fe1ea3717b62df8c4320 | branch:   origin/HEAD -> origin/master
tag-from-feature-branch: 851cb526e9f9e2c00a3b1e626c010038ecab7428 | branch:   origin/feature/DPTGROGU-0000-support

Given the snippet above you can use any bash tool to filter related to a specific branch.


  1. If you have the name of the branch and want to know which tags belong to that branch we can use a more fancy solution
git tag --merged branch

试试这个:

branch=$(git for-each-ref | grep ${commit_num} | grep origin | sed "s/.*\///")

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