Git: 如何找出标记在哪个分支上?

我目前正忙于一个项目与许多分支,我有一个最后的变化,在其中一个分支完成的标签。但我不清楚这个标签在哪个树枝上。

如何找出标签在哪个分支上?

89712 次浏览

With a Tag you mark a reference. So when you are on a dev branch and Tag this state. Your tag is on the actual reference. So in this case you can look to Gitk or another tool where the tree is shown. There you can see on which reference the Tag is.

Git: 有类似于每个分支标签的东西吗?
Http://git-scm.com/book/en/git-basics-tagging

这里有一个很好的解释。

甚至更短:

git branch --contains tags/<tag>

(它适用于任何类似树的参考)


If you can find 提交标记所引用的:

 git rev-parse --verify tags/<tag>^{commit}
# or, shorter:
git rev-parse tags/<tag>~0

然后你可以找到 哪个分支包含提交

git branch --contains <commit>

As 评论 below by User3356885, for the fetched branches (branches in remotes namespace)

git branch -a --contains tags/<tag>
git branch -a --contains <commit>

As noted in Pyr3z's 回答, for each candidate tag listed above, you can add:

git log -1 --pretty='%D' TAG

它将显示与该标记关联的分支。

对于@VonC 关于查找标记引用的提交的注释,只需使用:

git show <tag>

由于标记与特定的提交绑定在一起,因此可以使用它来显示提交——这将提供完整的提交详细信息。

git branch --contains tag

对我没有任何帮助,但是我在 Git gui中找到了解决这个问题的方法。

这样开始:

git gui

(在我的 Ubuntu 上,我必须先用 sudo apt-get install git-gui安装它。)

然后我选择菜单项 知识库-> 可视化所有分支历史。在结果窗口中,我选择菜单项 文件-> 列表引用

另一个窗口弹出,列出了我所有的标签(和其他引用)。这些都是可点击的,点击其中之一后,我只需要检查左下角的分支列表框架。像这样:

Parent: somesha (message)
Parent: someothersha (another message)
Child:  anothersha (yet another message)
Branches: branch1, master, remotes/origin/branch2, remotes/upstream/branch1, etc
Follows: v1.1.2
Precedes: v1.1.4

如果“ git Branch —— include”什么都不做,请确保包含了所有分支,包括远程分支和本地分支:

git branch -a --contains <tag>

From the git help:

Specific git-branch actions: -a, --all list both remote-tracking and local branches

标记总是引用提交号。使用这个标记号,您可以找到放置标记的分支:

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

你也可以试试这个,有相似的用例,这对我很有用

git ls-remote --heads origin | grep $CI_COMMIT_SHORT_SHA  | sed "s/.*\///"

Slightly different but taking inspiration from @ttfreeman's answer

步骤1获取提交 id:

git show {tag name}

复制提交 id 并粘贴以获得所有分支:

By example:
git branch --contains 94a152c2d1c6830c5a044ecf20526d51e64bda83

我的问题在这里得到了答案

ー特别是解决方案,例如

git branch -a --contains TAG

以及类似的问题是能够在输出中列出多个分支,并且不清楚标记实际上起源于哪个分支:

$  git branch --contains TAG
branch-A
branch-B
* branch-C
branch-D

(*表示当前分支 = 不相关)

哦,使用 --sort=-committerdate=-taggerdate进行排序并不能完全澄清原始分支,因为这些 ref 属性可以通过与所讨论的 TAG 无关的操作进行更新。

git show TAG

对于我的问题(“这个标签是在哪个分支上创建的?”) ,他给出了真正的答案然而,缺省情况下 git show格式相当庞大,我所寻找的是一种高效的、机器友好的输出格式,以便传递给一些自动化脚本。

因此,git log是这项工作的核心命令:

git log -1 --format='%D' TAG

这样可以得到如下输出:

tag: TAG, origin/branch-B, branch-B

这正好告诉了我们标记所在的分支,而且机器可读性更高。