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

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

回答1

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 -n 1 $TAG

NOTE This works for both Annotated and Unannotated tags

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

[alias]
  tagcommit = rev-list -n 1

And then call it with:

$ git tagcommit $TAG

Possible pitfall: if you have a local checkout or a branch of the same tag name, this solution might get you "warning: refname 'myTag' is ambiguous". In that case, try increasing specificity, e.g.:

$ git rev-list -n 1 tags/$TAG

 

Why not use git rev-parse <tag>? Or git rev-list -1 <tag>? – Jakub Narębski Dec 7 '09 at 23:47
@ Jakub: git rev-parse $TAG returns the SHA1 of the tag object, not the commit to which it points. git rev-list -1 works, though. – mipadi Dec 8 '09 
 
 
回答2

WARNING This only works for Unannotated tags Therefore it is safer to use the accepted answer which works in the general case https://stackoverflow.com/a/1862542/1586965

git show-ref --tags

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

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

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(41)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2020-07-07 RollingFileAppender bufferSize is not working? Here is an alternative
2020-07-07 SSH Protocol
2016-07-07 让git for windows记住密码
2016-07-07 Linq的Except
2016-07-07 Possible multiple enumeration of IEnumerable
2015-07-07 Windows Communication Foundation from msdn
2015-07-07 WCF - Architecture
点击右上角即可分享
微信分享提示