如何判断标记指向 Git 中的哪个提交?

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

How to tell which commit a tag points to in Git?

gitgit-tag

提问by Igor Zevaka

I have a bunch of unannotated tags in the repository and I want to work out which commit they point to. Is there a command that that will just list the tags and their commit SHAs? Checking out the tag and looking at the HEAD seems a bit too laborious to me.

我在存储库中有一堆未注释的标签,我想弄清楚它们指向哪个提交。是否有只列出标签及其提交 SHA 的命令?检查标签并查看 HEAD 对我来说似乎有点太费力了。

Update

更新

I realized after I went through the responses that what I actually wanted was to simply look at the history leading up to the tag, for which git log <tagname>is sufficient.

看完这些回复后,我意识到我真正想要的是简单地查看导致标记的历史记录,这git log <tagname>已经足够了。

The answer that is marked as answer is useful for getting a list of tags and their commits, which is what I asked. With a bit of shell hackery I'm sure it's possible to transform those into SHA+Commit message.

标记为答案的答案对于获取标签列表及其提交很有用,这就是我所问的。通过一些 shell 黑客,我确信可以将它们转换为 SHA+Commit 消息。

采纳答案by mipadi

One way to do this would be with git rev-list. The following will output the commit to which a tag points:

一种方法是使用git rev-list. 以下将输出标记指向的提交:

$ git rev-list -n 1 $TAG

You could add it as an alias in ~/.gitconfigif you use it a lot:

~/.gitconfig如果你经常使用它,你可以将它添加为别名:

[alias]
  tagcommit = rev-list -n 1

And then call it with:

然后调用它:

$ git tagcommit $TAG

回答by CB Bailey

git show-ref --tags

For example, git show-ref --abbrev=7 --tagswill show you something like the following:

例如,git show-ref --abbrev=7 --tags将显示如下内容:

f727215 refs/tags/v2.16.0
56072ac refs/tags/v2.17.0
b670805 refs/tags/v2.17.1
250ed01 refs/tags/v2.17.2

回答by Hlung

Just use git show <tag>

只需使用 git show <tag>

However, it also dumps commit diffs. To omit those diffs, use git log -1 <tag>. (Thanks to @DolphinDream and @demisx !)

但是,它也会转储提交差异。要省略这些差异,请使用git log -1 <tag>. (感谢@DolphinDream 和@demisx!)

回答by orip

On my repository, git show-ref TAGshows the tag's hash, not the hash of the commit it points to.

在我的存储库中,git show-ref TAG显示标签的哈希值,而不是它指向的提交的哈希值。

git show-ref --dereference TAGshows, additionally, the commit being pointed at.

git show-ref --dereference TAG此外,还显示了所指向的提交。

回答by orip

From Igor Zevaka:

来自伊戈尔·泽瓦卡

Summary

概括

Since there are about 4 almost equally acceptable yet different answers I will summarise all the different ways to skin a tag.

由于大约有 4 个几乎同样可接受但不同的答案,我将总结所有不同的标签外观。

  1. git rev-list -1 $TAG(answer). git rev-listoutputs the commits that lead up to the $TAGsimilar to git logbut only showing the SHA1 of the commit. The -1limits the output to the commit it points at.

  2. git show-ref --tags(answer) will show all tags (local and fetched from remote) and their SHA1s.

  3. git show-ref $TAG(answer) will show the tag and its path along with the SHA1.

  4. git rev-parse $TAG(answer) will show the SHA1 of an unannotated tag.

  5. git rev-parse --verify $TAG^{commit}(answer) will show a SHA1 of both annotated and unannotated tags. On Windows use git rev-parse --verify %TAG%^^^^{commit}(four hats).

  6. cat .git/refs/tags/*or cat .git/packed-refs(answer) depending on whether or not the tag is local or fetched from remote.

  1. git rev-list -1 $TAG答案)。git rev-list输出导致$TAG类似git log但仅显示提交的 SHA1 的提交。将-1输出限制为它指向的提交。

  2. git show-ref --tags( answer) 将显示所有标签(本地和从远程获取)及其 SHA1。

  3. git show-ref $TAG( answer) 将显示标签及其路径以及 SHA1。

  4. git rev-parse $TAG( answer) 将显示未注释标签的 SHA1。

  5. git rev-parse --verify $TAG^{commit}( answer) 将显示带注释和未注释标签的 SHA1。在 Windows 上使用git rev-parse --verify %TAG%^^^^{commit}(四顶帽子)。

  6. cat .git/refs/tags/*cat .git/packed-refs答案)取决于标签是本地的还是从远程获取的。

回答by Jakub Nar?bski

Use

git rev-parse --verify <tag>^{commit}

(which would return SHA-1 of a commit even for annotated tag).

(即使对于带注释的标签,它也会返回提交的 SHA-1)。



git show-ref <tag>would also work if <tag>is not annotated. And there is always git for-each-ref(see documentation for details).

git show-ref <tag>如果<tag>没有注释也可以工作。并且总是存在git for-each-ref(有关详细信息,请参阅文档)。

回答by Tuong Le

How about this:

这个怎么样:

git log -1 $TAGNAME

OR

或者

git log -1 origin/$TAGNAME

回答by Daniel Little

In order to get the sha/hash of the commit that a tag refers to (not the sha of the tag):

为了获得标签引用的提交的 sha/hash(不是标签的 sha):

git rev-list -1 <tag>

git rev-list -1 <tag>

回答by gahooa

I'd also like to know the "right" way, but in the meantime, you can do this:

我也想知道“正确”的方式,但与此同时,你可以这样做:

git show mytag | head -1    

回答by Terrence Reilly

Even though this is pretty old, I thought I would point out a cool feature I just found for listing tags with commits:

尽管这已经很老了,但我想我会指出我刚刚发现的一个很酷的功能,用于列出带有提交的标签:

git log --decorate=full

It will show the branches which end/start at a commit, and the tags for commits.

它将显示以提交结束/开始的分支,以及提交的标签。