简体   繁体   English

如何从 Git 中的标签名称中获取分支?

[英]How to get the branch from tag name in Git?

I am trying to get the branch name from a given tag.我正在尝试从给定标签中获取分支名称。 I tried git describe --tag <tag-name> but this doesn't provide me any branch name.我试过git describe --tag <tag-name>但这并没有为我提供任何分支名称。 I tried using gitk to show to the tags and branch but gitk doesn't show me the tag.我尝试使用gitk显示标签和分支,但 gitk 没有显示标签。 When I list out the tags git tag -l I see the tag name exist in the list.当我列出标签git tag -l我看到标签名称存在于列表中。

I just want a simple command which can tell me the branch when tag name is provided.我只想要一个简单的命令,它可以在提供标签名称时告诉我分支。

Tags and Branches in Git are only Labels pointing to specific snapshot of your files. Git 中的标签和分支只是指向文件特定快照的标签。 That being said, Tags ain't linked to a branch, but to a commit.话虽如此,标签不是链接到分支,而是链接到提交。

As so, you have to check which branch contain the commit pointed by your tag, as so:因此,您必须检查哪个分支包含您的标签指向的提交,如下所示:

git branch --contains <tag name>

A tag is just an alias name for a commit.标签只是提交的别名。

That said, this should answer your question: https://stackoverflow.com/a/2707110/550177也就是说,这应该回答你的问题: https ://stackoverflow.com/a/2707110/550177

如果您知道标签链接到的提交编号,您可以通过以下命令找到放置标签的分支:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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