git 如何获取当前提交的标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2324195/
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 get tags on current commit
提问by Shawn
I have a repository which has multiple tags on the same commit. For example:
我有一个存储库,它在同一个提交上有多个标签。例如:
commit #3 <--- TAG1 / TAG2 / TAG3
|
commit #2 <--- TAG4/ TAG5
|
commit #1 <--- TAG6/ TAG7
I'd like to find out what tags are on a particular commit. For example, if I check commit 1, I'd like to get tag 6 and tag 7.
我想找出特定提交上的标签。例如,如果我检查提交 1,我想获得标签 6 和标签 7。
I have tried:
我试过了:
git checkout <commit 1>
git tag --contains
which displayed tags 1-7.
其中显示标签 1-7。
git checkout <commit 1>
git describe --tags HEAD
displayed tag 6 only.
仅显示标签 6。
What is the proper way to do this in Git?
在 Git 中执行此操作的正确方法是什么?
回答by Frederic Maria
For completion (thanks to Ciro Santili answer), git tag
has got the option --points-at
that does exactly what OP is asking.
为了完成(感谢 Ciro Santili 的回答),git tag
已经得到了--points-at
完全符合 OP 要求的选项。
git tag --points-at HEAD
It does not have the effect of also listing the tags put on forward commits (as Jonathan Hartley stated in his comment regarding git tag --contains
).
它不会同时列出放置在前向提交上的标签(正如 Jonathan Hartley 在他关于 的评论中所述git tag --contains
)。
回答by arcticmac
I guess maybe git has had some options added since this question was asked, but since it still comes in pretty high on google, I thought I'd add that this way works nicely:
我想也许自从提出这个问题以来,git 已经添加了一些选项,但是由于它在 google 上的排名仍然很高,我想我会补充说这种方式效果很好:
git tag -l --contains HEAD
Or replace HEAD
with any other valid commit reference you like.
或者替换HEAD
为您喜欢的任何其他有效提交参考。
This will print a newline separated list of tags if the HEAD contains any tags, and print nothing otherwise, so you would get:
如果 HEAD 包含任何标签,这将打印一个换行符分隔的标签列表,否则不打印任何内容,因此您将获得:
TAG6
TAG7
And of course there are lots of nice ways with various other shell tools that you can format that output once you have it...
当然,还有许多其他各种 shell 工具的好方法,您可以在拥有它后格式化输出...
回答by Josh Lee
Some improvements on William's answer:
对威廉回答的一些改进:
git config --global alias.tags 'log -n1 --pretty=format:%h%d'
The output looks like this:
输出如下所示:
~$ git tags
7e5eb8f (HEAD, origin/next, origin/master, origin/HEAD, master)
~$ git tags HEAD~6
e923eae (tag: v1.7.0)
回答by qoomon
git tag --points-at
git tag --points-at
--points-at
Only list tags of the given object (HEAD if not specified). Implies --list.
--指向
仅列出给定对象的标签(如果未指定,则为 HEAD)。暗示--list。
回答by webmat
This displays the commit id of HEAD, as well as any branches or any tags that also happen to be exactly at HEAD.
这将显示 HEAD 的提交 ID,以及恰好位于 HEAD 的任何分支或任何标签。
git reflog --decorate -1
Sample output:
示例输出:
484c27b (HEAD, tag: deployment-2014-07-30-2359, master, origin/master) HEAD@{0}: 484c27b878ca5ab45185267f4a6b56f8f8d39892: updating HEAD
回答by William Pursell
This is not ideal, but perhaps helpful:
这并不理想,但可能有帮助:
$ git log -n 1 --decorate --pretty=oneline
You could play around with the format to get exactly what you want.
您可以使用格式来获得您想要的内容。
回答by Jonathan Hartley
Here's a refinement of @JoshLee's answer, which manipulates the output to list only tags (not branches, nor HEAD) and strips the word 'tag:' and decorative punctuation. This is useful if you are scripting something up which needs to find the current tags (e.g. put them in your prompt):
这是@JoshLee 答案的改进,它操纵输出以仅列出标签(不是分支,也不是 HEAD)并去掉“标签:”这个词和装饰性标点符号。如果您正在编写需要查找当前标签的脚本(例如,将它们放在提示中),这将非常有用:
git log -n1 --pretty="format:%d" | sed "s/, /\n/g" | grep tag: | sed "s/tag: \|)//g"
Example output:
示例输出:
$ git log -n 1 --decorate=short
commit a9313...c7f2 (HEAD, tag: v1.0.1, tag: uat, mybranch)
...
$ git log -n1 --pretty="format:%d" | sed "s/, /\n/g" | grep tag: | sed "s/tag: \|)//g"
v1.0.1
uat
回答by Fran?ois
I'm doing git tag -l | grep $(git describe HEAD)
it returns the tag of the latest commit, or nothing if the last commit isn't tagged
我正在做git tag -l | grep $(git describe HEAD)
它返回最新提交的标签,如果最后一次提交没有标记,则不返回任何内容