如何判断给定的 git 标签是带注释的还是轻量级的?

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

How can I tell if a given git tag is annotated or lightweight?

git

提问by G. Sylvie Davies

I type git tagand it lists my current tags:

我输入git tag并列出我当前的标签:

1.2.3
1.2.4

How can I determine which of these is annotated, and which is lightweight?

我如何确定哪些是注释的,哪些是轻量级的?

采纳答案by jthill

git for-each-reftells you what each ref is to by default, its id and its type. To restrict it to just tags, do git for-each-ref refs/tags.

git for-each-ref告诉你每个 ref 默认是什么,它的 id 和它的类型。要将其限制为仅标签,请执行git for-each-ref refs/tags.

[T]he output has three fields: The hash of an object, the type of the object, and the name in refs/tags that refers to the object. A so-called "lightweight" tag is a name in refs/tags that refers to a commitobject. An "annotated" tag is a name in refs/tags that refers to a tagobject.

- Solomon Slow(in the comments)

[T]he 输出具有三个字段:对象的哈希值、对象的类型以及引用该对象的 refs/tags 中的名称。所谓的“轻量级”标签是 refs/tags 中引用commit对象的名称。“带注释的”标签是 refs/tags 中引用tag对象的名称。

-所罗门慢(在评论中)

回答by G. Sylvie Davies

The git show-ref -d --tagscommand sort of does it, since lightweight tags occur once in the output, and annotated tags occur twice. Also, only annotated tags include the "^{}" dereference operator in the output.

git show-ref -d --tags命令是这样做的,因为轻量标签在输出中出现一次,而带注释的标签出现两次。此外,只有带注释的标签在输出中包含“^{}”取消引用运算符。

588e9261795ec6dda4bd0a881cf1a86848e3d975 refs/tags/1.2.3
7fe2caaed1b02bb6dae0305c5c0f2592e7080a7a refs/tags/1.2.4
588e9261795ec6dda4bd0a881cf1a86848e3d975 refs/tags/1.2.4^{}

And that output can than be massaged with the unix sort, sed, cut, and uniq commands to make the output more readable:

然后可以使用 unix sort、sed、cut 和 uniq 命令处理该输出,以使输出更具可读性:

git show-ref -d --tags       | 
cut -b 42-                   | # to remove the commit-id
sort                         |
sed 's/\^{}//'               | # remove ^{} markings
uniq -c                      | # count identical lines
sed 's/2\ refs\/tags\// a /' | # 2 identicals = annotated
sed 's/1\ refs\/tags\//lw /'   

For my original repo (from my question) it outputs this:

对于我的原始回购(来自我的问题),它输出以下内容:

  lw 1.2.3
   a 1.2.4

(e.g., 1.2.3 was "lightweight" and "1.2.4" was annotated).

(例如,1.2.3 是“轻量级”,而“1.2.4”被注释)。

回答by Noufal Ibrahim

Get the tag name (say foo) and then do a git cat-file -t foo. If it's an an annotated tag, cat-filewill tell you that it's a "tag". If it's a simple tag, cat-filewill tell you that it's a "commit".

获取标签名称(例如foo),然后执行git cat-file -t foo. 如果它是一个带注释的标签,cat-file会告诉你它是一个“标签”。如果它是一个简单的标签,它cat-file会告诉你这是一个“提交”。

Update: As oxymoron said in his comment, git showworks too but it gives you more information than just what kind of tag it is.

更新:正如矛盾修辞法在他的评论中所说,git show它也有效,但它为您提供的信息不仅仅是它是什么类型的标签。

回答by Oxymoron

Please try using git describe

请尝试使用 git describe

https://git-scm.com/docs/git-describe

https://git-scm.com/docs/git-describe

By default (without --all or --tags) git describe only shows annotated tags.

默认情况下(没有 --all 或 --tags) git describe 只显示带注释的标签。