简体   繁体   English

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

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

For releases I usually tag with something like v1.1.0.对于版本,我通常使用 v1.1.0 之类的标记。 During my build script I am creating a fwVersion.c file that contains the current git info.在我的构建脚本中,我创建了一个包含当前 git 信息的 fwVersion.c 文件。 Currently, I have commit, and branch info in the file, but I would like to add the tag.目前,我在文件中有提交和分支信息,但我想添加标签。

Is this possible?这可能吗?

Check the documentation for git describe .检查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.它找到与给定提交最近的标签(即指向提交祖先的标签)并根据标签描述该提交。

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>

If what you want is the first tag containing the commit then:如果你想要的是包含提交的第一个标签,那么:

git describe --contains <commit>

gives the best answer IMO.给出了最好的答案 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.如果您有频繁的标签而不是在大型存储库中的旧提交上使用“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 运行得非常快,并为您提供一个输出值,它是包含提交的第一个标签以及您提交的回溯时间。

How about this?这个怎么样?

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

It gives you all the tags the given commit has (whereas git describe only gives you one), and does not include tags on descendant commits (like git tag --contains does).它为您提供给定提交具有的所有标签(而git describe只给您一个),并且不包括后代提交上的标签(如git tag --contains所做的那样)。

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

git tag --contains <commit>

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

Gives you:给你:

$ ReleaseBeta

Consolidating some of the answers:综合一些答案:

git tag --contains [<ref>]

and

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

or just要不就

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>]

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 --tags except that it only prints annotated tags. git describe [<ref>]行为与--tags相同,只是它只打印带注释的标签。

Supplying the option --contains to describe will print the a tag which is associated with an ancestor of the specified commit.提供选项--containsdescribe将打印与指定提交的祖先相关联的标签。 For example例如

$ 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM