如何列出未推送的Git标签

31

我想查看我本地有哪些标签在特定的远程仓库中不可用。我该怎么做?我知道可以使用git push --tags命令将所有标签推送到远程仓库,但是如果有一些标签我不想推送,该如何确保没有遗漏任何标签呢?

3个回答

44
您可以使用以下内容查看本地存在但未在指定的远程库中的标记:
git show-ref --tags | grep -v -F "$(git ls-remote --tags <remote name> | grep -v '\^{}' | cut -f 2)"
请注意,git ls-remote命令会显示带注释的标签和指向其所指对象的提交信息,因此我们需要使用^{}删除重复内容。
另一种选择是使用--dry-run/-n参数来执行git push命令。
git push --tags --dry-run

这将显示将要推送的更改,但不会实际进行这些更改。


2
由于您正在使用 git ls-remote 的输出来从 show-ref 中过滤出结果,因此留下 ^{} 行不会有太大的影响。这样我们就可以得到一个稍微简单一些的命令:git show-ref --tags | grep -v -F "$(git ls-remote --tags origin | cut -f 2)" - phinze
3
这是完全相同的命令,只是将 <remote name> 替换为 origin。使用此命令进行复制/粘贴:git show-ref --tags | grep -v -F "$(git ls-remote --tags origin | grep -v '\^{}' | cut -f 2)" - funroll

2

记录一下,我正在使用'comm'命令的变体:

comm -23 <(git show-ref --tags | cut -d ' ' -f 2) <(git ls-remote --tags origin | cut -f 2)

我将其作为git别名放在.gitconfig文件中,使用适当的bash引用方式如下:

[alias]
    unpushed-tags = "!bash -c \"comm -23 <(git show-ref --tags | cut -d ' ' -f 2) <(git ls-remote --tags origin | cut -f 2)\""

comm -23 <(git ls-remote --tags .) <(git ls-remote --tags origin)。您可以通过本地路径或URL或Git的扩展URL样式传输方法指定存储库。 - jthill

1
我发现Ben Lings所提供的被接受的答案忽略了与远程标签部分匹配的未推送标签;例如,如果远程标签叫做“snowba”或“snow”,则未推送标签“snowball”将不会列出。
我制作了一个版本来检查当前检出分支中本地标签和远程库中标签之间的精确名称匹配,以查找未推送的标签:
comm -23 <(echo "$(git tag --list)") <(echo "$(git ls-remote --tags -q | grep -v '\^{}' | cut -f 2 | cut -d '/' -f 3-)") | paste -s -d " " -

如果您只想检查当前检出的分支中是否有未推送的标签:

comm -23 <(echo "$(git tag --merged)") <(echo "$(git ls-remote --tags -q | grep -v '\^{}' | cut -f 2 | cut -d '/' -f 3-)") | paste -s -d " " -

这里同样的查询未推送标签的当前分支被拆成了多个语句,以便在bash脚本中使用(并增加了可读性):
local_tags_in_current_branch="$(git tag --merged)"
remote_tags="$(git ls-remote --tags -q | grep -v '\^{}' | cut -f 2 | cut -d '/' -f 3-)"
unpushed_tags=`comm -23 <(echo "$local_tags_in_current_branch") <(echo "$remote_tags") | paste -s -d " " -`

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接