tags:

views:

383

answers:

7

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 labourious to me.

Edit: I realised 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.

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.

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

  1. git rev-list $TAG | head -n 1 (thanks mipadi). git rev-list outputs the commits that lead up to the $TAG similar to git log but only showing the SHA1 of the commit. For non-unix heads like myself head -n 1 will show the first line of the output, which is the tip of the tag.

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

  3. git show-ref $TAG(thanks Jakub Narębski) will show the tag and its path along with the SHA1.

  4. git rev-parse $TAG(thanks Jakub Narębski) will show the SHA1 of an unannotated tag.

  5. git rev-parse --verify $TAG^{commit} (thanks Jakub Narębski) 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 (thanks The MYYN) depending on whether or not the tag is local or fetched from remote.

A: 

This doesn't show the filenames, but at least you get a feel of the repository.

cat .git/refs/tags/*

Each file in that directory contains a commit SHA pointing to a commit.

Peter Stuifzand
This didn't work as, I think, I pulled the tags from remote. `.git/packed-refs` did work though.
Igor Zevaka
+1  A: 

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

git show mytag | head -1
gahooa
+2  A: 

i'd also like to know the right way, but you can always peek either into:

$ cat .git/packed-refs

or:

$ cat .git/refs/tags/*
The MYYN
Right, so behaviour for packed-refs and refs/tags is somewhat different, packed-refs is a text file containing tags and SHAs, whereas refs/tags/ is a directory with text files named after a tag containing the SHA. I actually think that the *proper* way of doing this is with `git rev-list`.
Igor Zevaka
+5  A: 

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

$ git rev-list $TAG | head -n 1

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

[alias]
  tagcommit = !sh -c 'git rev-list $0 | head -n 1'

And then call it with:

$ git tagcommit $TAG
mipadi
Interesting. +1
VonC
I've actually found that on windows (in bash shell and normal cmd line client) I don't need to do `| head -n 1` as rev-list just returns the SHA.
Igor Zevaka
Why not use `git rev-parse <tag>`? Or `git rev-list -1 <tag>`?
Jakub Narębski
@ 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
Thanks for hint on using aliases.
Ton van den Heuvel
@mipadi: For **un-annotated** tags it soedn't matter; for annotated tags you can use `git rev-parse $TAG^{commit}` or `git rev-parse $TAG^{}` to dereference annotated/signed tag
Jakub Narębski
+6  A: 
git show-ref --tags
Charles Bailey
Thanks for that, this actually hides the semantics of looking at either `.git/packed-refs` or `.git/refs/tags/*`
Igor Zevaka
+5  A: 

Use

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

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


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

Jakub Narębski
A: 

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

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

orip