33

So far I have:

git rev-parse <tagname> | xargs git cat-file -p

but this isn't the easiest thing to parse. I was hoping for something similar to git-log's --pretty option so I could grab just the info I need.

Any ideas?

1
  • git cat-file -p <tagname> works in git 2.44.
    – xmedeko
    Commented May 13 at 13:18

4 Answers 4

44

A more direct way of getting the same info is:

git cat-file tag <tagname>

This uses a single command and avoids the pipe.

I used this in a bash script as follows:

if git rev-parse $TAG^{tag} -- &>/dev/null
then
    # Annotated tag
    COMMIT=$(git rev-parse $TAG^{commit})
    TAGGER=($(git cat-file tag $TAG | grep '^tagger'))
    N=${#TAGGER} # Number of fields
    DATE=${TAGGER[@]:$N-2:2} # Last two fields
    AUTHOR=${TAGGER[@]:1:$N-3} # Everything but the first and last two
    MESSAGE=$(git cat-file tag $TAG | tail -n+6)
elif git rev-parse refs/tags/$TAG -- &>/dev/null
then
    # Lightweight tag - just a commit, basically
    COMMIT=$(git rev-parse $TAG^{commit})
else
    echo "$TAG: not a tag" >&2
fi
1
  • I think the newer answer using git for-each-ref is a better solution nowadays Commented May 21, 2021 at 20:57
35

git show $TAG will show you the information for the tag, as well as the commit it points to.

If you have something that already works for you, but is unwieldy to type, you could always set an alias:

[alias]
        showtag = !sh -c 'git rev-parse $1 | xargs git cat-file -p' -

And call it with:

$ git showtag my-tag-name
3
  • 1
    Thanks. I should have mentioned I also managed to get as far as git show --quiet --pretty="format:" $TAG but that's mostly the same as above.
    – quornian
    Commented Nov 15, 2010 at 15:35
  • @quornian: You could use Git's alias functionality. I provided an example in my answer.
    – mipadi
    Commented Nov 15, 2010 at 18:09
  • thanks, it works well! Just want to remind the readers that alias need to be added to file .gitconfig, normally it's in your home folder. And if you don't have a newline after this git showtag executed, you can add it by: showtag = !sh -c 'git rev-parse $1 | xargs git cat-file -p && echo ""' -
    – gary
    Commented Oct 11, 2018 at 1:25
14

This has already been answered a long time ago but still is the top search result even though it's not the best solution anymore, so here it goes:

Command:

git for-each-ref refs/tags/$TAG --shell --format='
TAG=%(refname)
TYPE=%(objecttype)
COMMIT=%(objectname)
TAGGER=%(tagger)
EMAIL=%(taggeremail)
DATE=%(taggerdate)
CONTENTS=%(contents)
'

--shell does the quoting for Shell scripts. There is also --perl, --python and --tcl. If you don't want to write whole format as a command line option, you can also put it in a file.txt and do this:

git for-each-ref refs/tags/<tag> --shell --format="$(cat file.txt)"

Output:

TAG='refs/tags/4.1.0-RC1'
TYPE='tag'
COMMIT='973cc103f942330550866588177fe53ea5765970'
TAGGER='ml_'
EMAIL='<[email protected]>'
DATE='Fri Sep 16 14:14:50 2016 +0200'
CONTENTS='Release 3:
* INSTALL.md added.
* GIT.md modified.
'

More information here: https://git-scm.com/docs/git-for-each-ref

2
  • I think this is the best answer now (better than my accepted answer) Commented Apr 29, 2021 at 22:50
  • 1
    Note that TYPE will be commit for a lightweight tag. Commented Apr 29, 2021 at 22:50
1

git tag --format knows the same formatting like `git for-each-ref FIELD NAMES, so it's possible to write e.g.:

git tag -l --format='%(tag) (taggername) %(taggeremail:mailmap,trim) %(taggerdate:iso-strict) %(contents)' <tagname>

Or list annotated tags only with their details:

git tag -l --omit-empty --format='%(if)%(tag)%(then)%(tag) %(taggername) %(taggeremail:mailmap,trim) %(taggerdate:iso-strict) %(contents:subject)%(end)'

Not the answer you're looking for? Browse other questions tagged or ask your own question.