Git Tag list, display commit sha1 hashes

2019-03-08 06:44发布

问题:

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 ?

回答1:

How about this?

git show-ref --tags


回答2:

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

I like this instead, which gives exactly what I want but can't get from 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 t and git tags as follows:

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 t as Git doesn't support calling an alias from another alias (which is a bummer).


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 --date option as well as fully control the output to match your unique taste in the --pretty option. Both options are well-documented in the git-log Documentation.



回答3:

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
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^{}


回答4:

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

git rev-parse tag1^0 tag2^0

It will print only the full SHA1s, on separate lines. The ^0 suffix 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.)

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



回答5:

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:

% 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 --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.

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



回答6:

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



回答7:

 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.