如何找到与给定 git commit 关联的标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1474115/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:06:26  来源:igfitidea点击:

How to find the tag associated with a given git commit?

git

提问by wes

For releases I usually tag with something like v1.1.0. During my build script I am creating a fwVersion.c file that contains the current git info. Currently, I have commit, and branch info in the file, but I would like to add the tag.

对于版本,我通常使用 v1.1.0 之类的标记。在我的构建脚本中,我创建了一个包含当前 git 信息的 fwVersion.c 文件。目前,我在文件中有提交和分支信息,但我想添加标签。

Is this possible?

这可能吗?

回答by CB Bailey

Check the documentation for git describe. It finds the nearest tag to a given commit (that is a tag which points to an ancestor of the commit) and describes that commit in terms of the tag.

检查文档git describe。它找到与给定提交最近的标签(即指向提交祖先的标签)并根据标签描述该提交。

If you only want to know if the commit is pointed to by a tag then you can check the output of:

如果您只想知道提交是否由标记指向,那么您可以检查以下输出:

git describe --exact-match <commit-id>

回答by Jay

If what you want is the first tag containing the commit then:

如果你想要的是包含提交的第一个标签,那么:

git describe --contains <commit>

gives the best answer IMO. If you have frequent tags than using "git tag --contains" on an old commit in a big repository can take a while to run and gives you all of the tags that contain that commit.

给出了最好的答案 IMO。如果您有频繁的标签而不是在大型存储库中的旧提交上使用“git tag --contains”可能需要一段时间才能运行并为您提供包含该提交的所有标签。

This form of git describe runs very quickly and gives you a single output value which is the first tag containing the commit and how far back your commit is.

这种形式的 git describe 运行得非常快,并为您提供一个输出值,它是包含提交的第一个标签以及您提交的回溯时间。

回答by Albertas Agejevas

How about this?

这个怎么样?

git tag --points-at <commit-id>

git tag --points-at <commit-id>

It gives you all the tags the given commit has (whereas git describeonly gives you one), and does not include tags on descendant commits (like git tag --containsdoes).

它为您提供给定提交具有的所有标签(而git describe只给您一个),并且不包括后代提交上的标签(就像这样git tag --contains做)。

回答by dharga

You can find this information in the manual

您可以在手册中找到此信息

git tag --contains <commit>

回答by Christopher Alexander

I found the combo of both top answers to give me what i wanted like so:

我找到了两个最高答案的组合,给了我我想要的东西:

git describe --tags --exact-match <commit-id>

This gives you the tag that is ONLY for that commit and for ones without annotation. Useful when you want to find tags and not worry about stripping the formatting off then (for Jenkins for example).

这为您提供了仅用于该提交和没有注释的标签。当您想查找标签而不用担心剥离格式时很有用(例如对于 Jenkins)。

eg. $ git describe --tags --exact-match head~2

例如。 $ git describe --tags --exact-match head~2

Gives you:

给你:

$ ReleaseBeta

回答by bschlueter

Consolidating some of the answers:

综合一些答案:

git tag --contains [<ref>]

git tag --contains [<ref>]

and

git tag --points-at [<ref>]

git tag --points-at [<ref>]

or just

要不就

git tag

git tag

behave the same, printing any (and all) tags for the specified ref or the current commit if not specified.

行为相同,如果未指定,则为指定的引用或当前提交打印任何(和所有)标签。

git describe --tags [<ref>]

git describe --tags [<ref>]

where <ref>defaults to the current commit, exits with 128 if no tags are associated with the commit, and prints a tag associated with the commit (there does not seem to be a pattern).

其中<ref>默认为当前提交,如果没有标签与提交关联,则以 128 退出,并打印与提交关联的标签(似乎没有模式)。

git describe [<ref>]behaves the same as with --tagsexcept that it only prints annotated tags.

git describe [<ref>]--tags除了只打印带注释的标签外,其 行为与 with相同。

Supplying the option --containsto describewill print the a tag which is associated with an ancestor of the specified commit. For example

提供选项--contains,以describe将其打印与指定提交的祖先相关的标签。例如

$ git init
Initialized empty Git repository in /tmp/test
$ git commit -m one --allow-empty
[master (root-commit) 7fdfff2] one
$ git commit -m two --allow-empty
[master cd5f8f1] two
$ git tag -am foo foo
$ git tag bar
$ git log --format=oneline
cd5f8f1f4f29eb164f83e224768ccaf37fe170ed (HEAD -> master, tag: foo, tag: bar) two
7fdfff2ce5e3347f8eee4c9f2413dbd4e90060e1 one
$ git describe 7fdfff2ce5e3347f8eee4c9f2413dbd4e90060e1
fatal: No tags can describe '7fdfff2ce5e3347f8eee4c9f2413dbd4e90060e1'.
Try --always, or create some tags.
$ git describe --contains 7fdfff2ce5e3347f8eee4c9f2413dbd4e90060e1
bar~1