简体   繁体   English

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

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

I type git tag and 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?我如何确定哪些是注释的,哪些是轻量级的?

git for-each-ref tells you what each ref is to by default, its id and its type. git for-each-ref告诉你每个 ref 默认是什么,它的 id 和它的类型。 To restrict it to just tags, do git for-each-ref refs/tags .要将其限制为仅标记,请执行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. [T]输出有三个字段:对象的哈希值、对象的类型以及引用该对象的引用/标签中的名称。 A so-called "lightweight" tag is a name in refs/tags that refers to a commit ¹ object.所谓的“轻量级”标签是 refs/tags 中的一个名称,它指的是一个commit ¹ 对象。 An "annotated" tag is a name in refs/tags that refers to a tag object. “带注释的”标签是引用tag对象的 refs/tags 中的名称。

- Solomon Slow (in the comments) -所罗门慢(在评论中)

Here is an example:下面是一个例子:

$ git for-each-ref refs/tags                                           
902fa933e4a9d018574cbb7b5783a130338b47b8 commit refs/tags/v1.0-light
1f486472ccac3250c19235d843d196a3a7fbd78b tag    refs/tags/v1.1-annot
fd3cf147ac6b0bb9da13ae2fb2b73122b919a036 commit refs/tags/v1.2-light

To do this for just one ref, you can use git cat-file -t on the local ref, to continue the example:要仅对一个 ref 执行此操作,您可以在本地 ref 上使用git cat-file -t以继续示例:

$ git cat-file -t v1.0-light
commit
$ git cat-file -t v1.1-annot
tag

¹ tags can refer to any Git object, if you want a buddy to fetch just one file and your repo's got a git server, you can git tag forsam :that.file and Sam can fetch it and show it. ¹ 标签可以引用任何 Git 对象,如果你想让一个伙伴只获取一个文件并且你的git tag forsam :that.file有一个 git 服务器,你可以git tag forsam :that.file并且 Sam 可以获取它并显示它。 Most of the convenience commands don't know what to do with tagged blobs or trees, but the core commands like update-index and such do大多数便利命令不知道如何处理标记的 blob 或树,但是像 update-index 这样的核心命令可以

The git show-ref -d --tags command sort of does it, since lightweight tags occur once in the output, and annotated tags occur twice. git show-ref -d --tags命令可以做到这一点,因为轻量标签在输出中出现一次,而带注释的标签出现两次。 Also, only annotated tags include the "^{}" dereference operator in the output.此外,只有带注释的标签在输出中包含“^{}”取消引用运算符。

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

(eg, 1.2.3 was "lightweight" and "1.2.4" was annotated). (例如,1.2.3 是“轻量级”,而“1.2.4”被注释)。

Get the tag name (say foo ) and then do a git cat-file -t foo .获取标签名称(比如foo ),然后执行git cat-file -t foo If it's an an annotated tag, cat-file will tell you that it's a "tag".如果它是一个带注释的标签, cat-file会告诉你它是一个“标签”。 If it's a simple tag, cat-file will tell you that it's a "commit".如果它是一个简单的标签, cat-file会告诉你这是一个“提交”。

Update: As oxymoron said in his comment, git show works too but it gives you more information than just what kind of tag it is.更新:正如矛盾修辞在他的评论中所说, git show可以工作,但它为您提供的信息不仅仅是它是什么类型的标签。

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 只显示带注释的标签。

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

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