Git Tag list, display commit sha1 hashes

2019-03-08 06:41发布

so the git tag command lists the current git tags

tag1
tag2

git tag -n prints tag's message

tag1  blah blah
tag2  blah blah

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

7条回答
我只想做你的唯一
2楼-- · 2019-03-08 07:24

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:

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
7208212a55c4a56af34da781a7f730d6ffffd557a1 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^{}
查看更多
登录 后发表回答