git tag --sort='creatordate' --merged
命令列出所有可从 HEAD 访问/到达的标签,并按时间顺序排序。git tag --sort='creatordate' --merged mybranch
。git tag --sort='creatordate' --merged mybranch
。引用可以是标签、分支、提交哈希等。 - qwertzguy要打印指向特定提交的所有标签,可以这样做:
git for-each-ref refs/tags | grep HASH
或者如果您使用的是Windows,而且没有使用Cygwin或类似工具:
git for-each-ref refs/tags | find "HASH"
如果你只想要标签名称,那么你不能使用 Git 的 --format
,因为我们需要它来进行 grep 操作。因此,在最后一步中(Linux/Cygwin),我们需要去掉我们不感兴趣的内容:
git for-each-ref refs/tags | grep HASH | sed -r "s/.*refs\/tags\/(.*)/\1/"
关于最初的问题:
这个代码会遍历所有分支上的所有标签,询问git哪些分支包含每个标签,并根据提供的分支名称进行筛选 - 但是要注意,它非常慢:
git for-each-ref refs/tags --format "%(refname)" | while read x
do
if git branch --contains $x | grep -q "^[ *] master$"
then echo $x
fi
done
下面的引用自另一个答案速度更快:
git log --simplify-by-decoration --decorate --pretty=oneline "master" | fgrep 'tag: '
...但是如果多个标签指向相同的提交,它将会产生:
(HEAD, tag: Test-Tag1, tag: Test-Tag2, Test-Tag3, fork/devel, devel)
(tag: Another-Tag)
(tag: And-Another)
(三个标签 Test-Tag*
指向同一个提交)
我编写了一个 Python 脚本,仅输出每行一个的标签名称(仅在 Windows 平台测试通过):
import os
from subprocess import call
print("-" * 80)
dirpath = r"D:\Projekte\arangodb" # << Put your repo root path here!
tagdir = os.path.join(dirpath, ".git", "refs", "tags")
commitspath = os.path.join(dirpath, "_commit_list.tmp")
# could probably read from stdin directly somewhow instead of writing to file...
# write commits in local master branch to file
os.chdir(dirpath)
os.system("git rev-list refs/heads/master > " + commitspath)
tags = {}
for tagfile in os.listdir(tagdir):
with open(os.path.join(tagdir, tagfile), "r") as file:
tags[file.read().strip()] = tagfile
tags_set = set(tags)
commits = {}
with open(commitspath, "r") as file:
for line in file:
commits[line.strip()] = 1
os.remove(commitspath)
commits_set = set(commits)
for commit in sorted(commits_set.intersection(tags_set), key=lambda x: tags[x]):
print(tags[commit])
结果:
Test-Tag1
Test-Tag2
Test-Tag3
Another-Tag
And-Another
最后一行可以修改为print(commit, tags[commit])
,这样就可以选择性地打印每个标签的提交哈希。顺便说一下,这个脚本似乎表现得非常好!
理想情况下,Git应该支持以下命令,以避免所有这些解决方法:
git tag --list --branch master
git for-each-ref refs/tags | grep HASH
。 - CodeManXgit tag --list --branch ...
和for-each-ref
结合使用加上grep
似乎是检索所有标签名称的唯一方法。 - CodeManX