Git 标签列表,显示提交 sha1 哈希值

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

Git Tag list, display commit sha1 hashes

gitlogginghashgit-tagverbose

提问by Quang Van

so the git tagcommand lists the current git tags

所以该git tag命令列出了当前的 git 标签

tag1
tag2

git tag -nprints tag's message

git tag -n打印标签的消息

tag1  blah blah
tag2  blah blah

What's the best way to get the hash of tag1 & tag2 ?

获取 tag1 和 tag2 的散列的最佳方法是什么?

回答by peterjmag

To get git tags with the SHA1 hash of the Tag object, you can run:

要使用 Tag 对象的 SHA1 哈希获取 git 标签,您可以运行:

git show-ref --tags

The output will then look something like:

输出将如下所示:

0e76920bea4381cfc676825f3143fdd5fcf8c21f refs/tags/1.0.0
5ce9639ead3a54bd1cc062963804e5bcfcfe1e83 refs/tags/1.1.0
591eceaf92f99f69ea402c4ca639605e60963ee6 refs/tags/1.2.0
40414f41d0fb89f7a0d2f17736a906943c05acc9 refs/tags/1.3.0

Each line is the SHA1 hash of the tag, followed by the tag name prefixed with refs/tags/.

每一行是标签的 SHA1 哈希值,后跟以 为前缀的标签名称refs/tags/

If you want the SHA1 hash of the commit, instead of the tag object, you can run:

如果你想要提交的 SHA1 哈希值,而不是标签对象,你可以运行:

git show-ref --tags -d

This will produce output like:

这将产生如下输出:

0e76920bea4381cfc676825f3143fdd5fcf8c21f refs/tags/1.0.0
3e233dd8080617685992dc6346f739a6f6396aae refs/tags/1.0.0^{}
5ce9639ead3a54bd1cc062963804e5bcfcfe1e83 refs/tags/1.1.0
09173980152a7ed63d455829553448ece76c6fdc refs/tags/1.1.0^{}
591eceaf92f99f69ea402c4ca639605e60963ee6 refs/tags/1.2.0
56d803caaa8a93a040b7be0b8a36abdc4ce8c509 refs/tags/1.2.0^{}
40414f41d0fb89f7a0d2f17736a906943c05acc9 refs/tags/1.3.0
1bdf628a70fda7a0d840c52f3abce54b1c6b0130 refs/tags/1.3.0^{}

The lines ending with ^{}start with the SHA1 hash of the actual commit that the tag points to.

^{}以标记指向的实际提交的 SHA1 哈希值开头的行。

回答by ADTC

The git tagcommand is underdeveloped. A lot is desired but missing in it, like full tag details and tags in the commit history order.

git tag命令不发达。很多是需要的,但其中缺少很多,例如完整的标签详细信息和提交历史顺序中的标签。

I like this instead, which gives exactly what I want but can't get from git tag:

我喜欢这个,它提供了我想要的但无法从中获得的东西git tag

git log --oneline --decorate --tags --no-walk

This gives a very nice color-coded view of the tags in the reverse chronological order (as it would be in the full log). That way, not only do you see the tags, you will also see the abbreviated hashes and the commit messages of the tag commits.

这提供了一个非常漂亮的标签颜色编码视图,按时间倒序排列(就像在完整日志中一样)。这样,您不仅会看到标签,还会看到缩写的哈希值和标签提交的提交消息。



I have aliased it to git tand git tagsas follows:

我已将其别名为git tgit tags如下:

git config --global alias.tags "log --oneline --decorate --tags --no-walk"
git config --global alias.t "!git tags"

Note:I had to use bash redirection for git tas Git doesn't support calling an alias from another alias (which is a bummer).

注意:我不得不使用 bash 重定向,git t因为 Git 不支持从另一个别名调用别名(这是一个无赖)。



If you want to see the commit date and time, try:

如果要查看提交日期和时间,请尝试:

git log --tags --no-walk --date=iso-local --pretty='%C(auto)%h %cd%d %s'

You can use other date formats in the --dateoption as well as fully control the output to match your unique taste in the --prettyoption. Both options are well-documented in the git-log Documentation.

您可以在--date选项中使用其他日期格式,也可以完全控制输出以匹配您在--pretty选项中的独特品味。这两个选项都在git-log Documentation 中有详细记录

回答by Steven Penny

Annotated tags have their own SHA?1, so we need to dereference them. However lightweight tags cannot be dereferenced, as they already point to a commit. To solve, we must list both and filter the commit objects:

带注释的标签有自己的 SHA?1,所以我们需要取消引用它们。然而,轻量级标签不能被取消引用,因为它们已经指向一个提交。要解决,我们必须列出并过滤提交对象:

git for-each-ref --sort -v:refname --format '%(objectname) %(objecttype) %(refname)
%(*objectname) %(*objecttype) %(*refname)' refs/tags | grep commit

Result with lightweight tags:

轻量级标签的结果:

589610a0114a375f1bff716dd308cf8df08571d3 commit refs/tags/1.4.9
e25952a74bf379783944bef9c4fcc60600cb764c commit refs/tags/1.4.8
19b1c2c96a9678837f57eac86cf3d22842731510 commit refs/tags/1.4.7
7208212a55c4a56af34da781a7f730d6ddd557a1 commit refs/tags/1.4.6
62ec20337a4125496bd4f56288f3283963153194 commit refs/tags/1.4.5

Result with annotated tags:

带注释标签的结果:

e2b2d6a172b76d44cb7b1ddb12ea5bfac9613a44 commit refs/tags/v2.11.0-rc3^{}
1310affe024fba407bff55dbe65cd6d670c8a32d commit refs/tags/v2.11.0-rc2^{}
3ab228137f980ff72dbdf5064a877d07bec76df9 commit refs/tags/v2.11.0-rc1^{}
1fe8f2cf461179c41f64efbd1dc0a9fb3b7a0fb1 commit refs/tags/v2.11.0-rc0^{}
454cb6bd52a4de614a3633e4f547af03d5c3b640 commit refs/tags/v2.11.0^{}

回答by Cascabel

To get the SHA1 referred to by any sort of ref (branch, tag...) use git rev-parse:

要获取任何类型的 ref(分支、标签...)引用的 SHA1,请使用git rev-parse

git rev-parse tag1^0 tag2^0

It will print only the full SHA1s, on separate lines. The ^0suffix is a special syntax, to ensure that this will print the SHA1 of the commit pointed to by the tag, whether it's annotated or not. (Annotated tags are objects in their own right, which contain a pointer to a commit along with metadata. If you do know a tag is annotated, and want the tag's SHA1, simply leave off the ^0.)

它只会在单独的行上打印完整的 SHA1。该^0后缀是一个特殊的语法,以确保这将打印SHA1由标签提交指向的,无论是注释与否。(带注释的标签本身就是对象,其中包含一个指向提交的指针以及元数据。如果您知道某个标签已被注释,并且想要该标签的 SHA1,只需去掉^0.)

Of course, you shouldn't often need to do this, since any Git command that would accept an SHA1 should also accept a tag!

当然,您不应该经常需要这样做,因为任何接受 SHA1 的 Git 命令也应该接受标签!

回答by Ted

I had a similar question, but wanted the hash of (several) specific tags. I found that "show-ref" will take a list of tags, so this does the job:

我有一个类似的问题,但想要(几个)特定标签的哈希值。我发现“show-ref”将采用标签列表,因此可以完成以下工作:

% git show-ref v3.4.0.13-ga v3.4.0.13-base
bfc7747c4cf67a4aacc71d7a40337d2c3f73a886 refs/tags/v3.4.0.13-base
79ba365e75a4f9cee074d25a605a26acb660b7de refs/tags/v3.4.0.13-ga

However, some experimentation with "git show" resulted in this command:

但是,对“git show”的一些实验导致了这个命令:

% git show --summary --oneline --decorate v3.4.0.13-ga v3.4.0.13-base
79ba365 (tag: v3.4.0.13-ga, rhins013a) commit message the first
bfc7747 (tag: v3.4.0.13-base) commit message the second

Since I'm much more familiar with using "show" than "show-ref", I find the latter easier to remember and more helpful too.

由于我更熟悉使用“show”而不是“show-ref”,我发现后者更容易记住,也更有帮助。

See also the nice summary in How to tell which commit a tag points to in Git?.

另请参阅如何分辨标记指向 Git 中的哪个提交?.

回答by Adam Dymitruk

The tags have to be signed and/or messaged. Lightweight tags don't have SHA1 objects and are just refs. Otherwise try git show.

必须对标签进行签名和/或发送消息。轻量级标签没有 SHA1 对象,只是参考。否则试试git show

回答by anatoly techtonik

 git for-each-ref --format='%(if)%(*objectname)%(then)%(*objectname)%(else)%(objectname)%(end) %(refname)' refs/tags

This gives a list of all commits for tags. Annotated tags are dereferenced. Send thanks here.

这给出了标签的所有提交的列表。注释标签被取消引用。在此致谢。

回答by Radon8472

I took the command from anatoly techtonik postadded the headline message of the tags/commits and formated it as nice cols.

我从anatoly techtonik post添加了标签/提交的标题消息并将其格式化为漂亮的 cols。

The result is a output identical to git tag -nbut with commit-hash as prefix.

结果是输出相同git tag -n但以 commit-hash 作为前缀。

git for-each-ref --format='%(if)%(*objectname)%(then)%(*objectname:short)%(else)%(objectname:short)%(end)|%(refname:short)|%(contents:subject)' refs/tags | column -t -s '|'

If you like to have the long-hash instead of the short, yust replace objectname:shortby objectname.

如果您喜欢使用长哈希而不是短哈希,只需将其替换objectname:shortobjectname.