如何列出某个 git 分支中的所有标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32166548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to list all tags within a certain git branch
提问by mkmostafa
I have a couple of branches in my git repo. I would like to know if there is a command that lists all the tags within a certain branch not all the tags in the whole repo.
我的 git repo 中有几个分支。我想知道是否有一个命令列出某个分支中的所有标签,而不是整个 repo 中的所有标签。
I tried git tag --contains
. but it didn't work as expected.
我试过了 git tag --contains
。但它没有按预期工作。
Image 1, there's a list of all the tags with their hashes (the marked two tags have the same hash/commit)
Image 2, as you can see I'm on a branch called "b"
Image 3, I queried which branch contains the hash of both tags (both have same hash) and it said they are on branch "b" (the one I'm currently on)
图 1,有一个包含所有标签及其散列的列表(标记的两个标签具有相同的散列/提交)
图 2,如您所见,我在一个名为“b”的分支上
图 3,我查询了哪个分支包含两个标签的哈希值(都具有相同的哈希值),并表示它们在分支“b”(我目前所在的分支)上
Image 4, describing the branch tags, It only gave me ONE tag
Image 5, also describing the tags of the hash that is supposed to point to the commit that is tagged with both tags, it only shows ONE tag again
回答by qwertzguy
Use git tag --sort='creatordate' --merged
to list all tags accessible/reachable from HEAD and sort them chronologically.
使用git tag --sort='creatordate' --merged
列出所有标签从头部接近/到达和排序他们按时间顺序。
You can specify a ref if you don't want HEAD, like git tag --sort='creatordate' --merged mybranch
如果你不想要 HEAD,你可以指定一个 ref,比如 git tag --sort='creatordate' --merged mybranch
回答by CodeManX
To print all tags, that point to a certain commit, you can do:
要打印指向某个提交的所有标签,您可以执行以下操作:
git for-each-ref refs/tags | grep HASH
Or if you are on Windows and don't use Cygwin or similar:
或者,如果您使用的是 Windows 并且不使用 Cygwin 或类似软件:
git for-each-ref refs/tags | find "HASH"
If you want the tag name only, you can't use Git's --format
, because we need it for grep'ing. Thus we need to strip the stuff we aren't interested in away in the final step (Linux/Cygwin):
如果您只想要标签名称,则不能使用 Git 的--format
,因为我们需要它来进行 grep'ing。因此,我们需要在最后一步(Linux/Cygwin)中去除我们不感兴趣的东西:
git for-each-ref refs/tags | grep HASH | sed -r "s/.*refs\/tags\/(.*)//"
Regarding the initial question:
关于最初的问题:
This iterates over all tags in all branches, asks git which branches contain each tag and filters based on the supplied branch name - but be warned, it's super slow:
这将遍历所有分支中的所有标签,根据提供的分支名称询问 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
The following taken from another answeris much faster:
以下来自另一个答案的速度要快得多:
git log --simplify-by-decoration --decorate --pretty=oneline "master" | fgrep 'tag: '
... but if multiple tags point to the same commit, it will produce:
...但如果多个标签指向同一个提交,它将产生:
(HEAD, tag: Test-Tag1, tag: Test-Tag2, Test-Tag3, fork/devel, devel)
(tag: Another-Tag)
(tag: And-Another)
(three tags Test-Tag*
pointing to the same commit)
(三个标签Test-Tag*
指向同一个提交)
I wrote a Python script that outputs tag names only, one per line (tested on Windows only):
我写了一个 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])
Result:
结果:
Test-Tag1
Test-Tag2
Test-Tag3
Another-Tag
And-Another
The commit hash could optionally be printed too for every tag, simply modify the last line to print(commit, tags[commit])
. The script seems to perform very well by the way!
也可以选择为每个标签打印提交哈希,只需将最后一行修改为print(commit, tags[commit])
. 顺便说一句,剧本似乎表现得很好!
Ideally, git would support something like the following command to avoid all these workarounds:
理想情况下,git 将支持类似以下命令的内容,以避免所有这些变通方法:
git tag --list --branch master