Git Tag list, display commit sha1 hashes
Asked Answered
T

8

143

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 ?

Tann answered 9/1, 2012 at 23:13 Comment(1)
Not a question I consider asking separately, but I wonder why git tag (-n or some other option...) does not show commit hashes in the first place. It's nice to see what solutions people come up with, but I consider this a design flaw in the available options.Adela
V
187

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

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

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

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.

Vivianviviana answered 9/1, 2012 at 23:26 Comment(3)
Note, this won't differentiate between lightweight and annotated tags. For lightweight tags it'll show the commit and for annotated tags it'll show the hash of the tag object itself.Jenny
To show a list of tags with dereferenced refs (in case of annotated tags) use git show-ref --tags -d. Dereferenced tags are postfixed with a ^{}.Harelip
Can we also show the tag message next to each of them?Uturn
P
76

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.

Passifloraceous answered 20/9, 2014 at 20:23 Comment(9)
Although not what the OP asked (display commit sha1 hashes) it's very useful since the commit messages might be handy as well. +1 from me.Ahlers
@nealmcb git log is powerful! You can make it show exactly what you want. Try git log --tags --no-walk --date=iso-local --pretty='%C(auto)%h %cd%d %s'. Other date formats are also possible. Just look up --date in the help page. I have updated my answer to include this option.Passifloraceous
@SteliosAdamantidis Actually my answer gives the abbreviated SHA1 hashes (first 7 characters), and if you want full-length hashes, you can always modify it with --pretty and %H. Thank you for the +1 :)Passifloraceous
For some reason I don't understand, on Windows (tested with git 2.32.0.windows.2), it is necessary to add a space changing %cd%d to %cd %d. It's only necessary running from a Windows command prompt - "git bash" on Windows doesn't have the problem. Full command for Windows cmd shell: git log --tags --no-walk --date=iso-local --pretty="%C(auto)%h %cd %d %s"Clemente
@Clemente Thanks for sharing. I didn't encounter it as I normally use Git Bash, not the Windows cmd shell. There are a lot of powerful things you can do with Bash that you simply can't on Command Prompt. Worth learning and switching over :)Passifloraceous
WRT Windows: The "%" is used for variable substitutions. %var%. To make it work on Windows without adding a space, escape the quotes and the %. git log --tags --no-walk --date=iso-local --pretty=^"^%C(auto)^%h ^%cd^%d ^%s^". Thats on the command line. In a BAT script, just double the % characters. git log --tags --no-walk --date=iso-local --pretty="%%C(auto)%%h %%cd%%d %%s"Stork
can we change coloring of the hash?Uturn
@Uturn Yes, you can use the variations of %C to change coloring. Custom colors will need to be defined in the configuration file. To stop a color application, use %Creset or %C(auto). Note that your terminal must support coloring (which modern terminals do).Passifloraceous
Is it possible to show the tag message next to each of them?Uturn
R
17

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^{}
Retaliate answered 30/11, 2012 at 8:11 Comment(1)
Using git log --tags --oneline --no-walk will also deference annotated tags automatically. :)Passifloraceous
G
13

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!

Gooey answered 9/1, 2012 at 23:35 Comment(1)
Best answer here, thanks @Jefromi. Note that in a Windows cmd shell any git command using ^ needs to be quoted:e.g. git rev-parse "tag1^0" "tag2^0".Congruence
S
8

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

Shower answered 28/9, 2012 at 16:11 Comment(0)
H
4
 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.

Heterodoxy answered 23/11, 2017 at 3:54 Comment(1)
Great solution. If you like to have Only tagenames instead of the full refname, you can change use %(refname:short) instead of %(refname).Acrostic
G
3

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

Gasaway answered 9/1, 2012 at 23:25 Comment(0)
A
3

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

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

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:short by objectname.

Acrostic answered 26/4, 2019 at 9:26 Comment(4)
Can we show dates for each tag?Uturn
I think this should be possible, take a look at the format options at git-scm.com/docs/git-for-each-ref#_examples . I think %(*authordate) is what you are looking forAcrostic
Can we also show date of the commit as well?Uturn
Did you read my last comment? Yes, i think it should be possible to show e.g. authordate too. Look at git-scm.com/docs/git-for-each-ref#_examples to see examples and format options for thisAcrostic

© 2022 - 2024 — McMap. All rights reserved.