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 laborious to me.
I realized 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.
Even though this is pretty old, I thought I would point out a cool feature I just found for listing tags with commits:
git log --decorate=full
It will show the branches which end/start at a commit, and the tags for commits.
In order to get the sha/hash of the commit that a tag refers to (not the sha of the tag):
git rev-list -1 <tag>
Short post-Git-2 answer
I know this question has been out here for quite a while. And the answer from CB Bailey is 100% correct: git show-ref --tags --abbrev
I like this one better since it uses git tag
:
git tag --list --format '%(refname:short) %(objectname:short)'
Simple. Short.
PS alias it as git taglist
with this command:
git config --global alias.taglist "tag --list --format '%(refname:short) %(objectname:short)'"
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.
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/*
I'd also like to know the "right" way, but in the meantime, you can do this:
git show mytag | head -1
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.
You could as well get more easy-to-interpret picture of where tags point to using
git log --graph |git name-rev --stdin --tags |less
and then scroll to the tag you're looking for via /
.
More compact view (--pretty=oneline
) plus all heads (-a
) could also help:
git log -a --pretty=oneline --graph |git name-rev --stdin --tags |less
Looks a bit terrifying, but could also be aliased in ~/.gitconfig
if necessary.
~/.gitconfig
[alias]
ls-tags = !git log -a --pretty=oneline --graph |git name-rev --stdin --tags |less
If you would like to see the details of the tag SOMETAG (tagger, date, etc), the hash of the commit it points to and a bit of info about the commit but without the full diff, try
git show --name-status SOMETAG
Example output:
tag SOMETAG
Tagger: ....
Date: Thu Jan 26 17:40:53 2017 +0100
.... tag message .......
commit 9f00ce27c924c7e972e96be7392918b826a3fad9
Author: .............
Date: Thu Jan 26 17:38:35 2017 +0100
.... commit message .......
..... list of changed files with their change-status (like git log --name-status) .....
Can use below, It will give the commit hash
git show -s --format=%H <tag>^{commit}
If abbreviated commit hash required, git show -s --format=%h <tag>^{commit}
How about this:
git log -1 $TAGNAME
OR
git log -1 origin/$TAGNAME
git show-ref --tags
For example, git show-ref --abbrev=7 --tags
will show you something like the following:
f727215 refs/tags/v2.16.0
56072ac refs/tags/v2.17.0
b670805 refs/tags/v2.17.1
250ed01 refs/tags/v2.17.2
From Igor Zevaka:
Since there are about 4 almost equally acceptable yet different answers I will summarise all the different ways to skin a tag.
git rev-list -1 $TAG
(answer). git rev-list
outputs the commits that lead up to the $TAG
similar to git log
but only showing the SHA1 of the commit. The -1
limits the output to the commit it points at.
git show-ref --tags
(answer) will show all tags (local and fetched from remote) and their SHA1s.
git show-ref $TAG
(answer) will show the tag and its path along with the SHA1.
git rev-parse $TAG
(answer) will show the SHA1 of an unannotated tag.
git rev-parse --verify $TAG^{commit}
(answer) will show a SHA1 of both annotated and unannotated tags. On Windows use git rev-parse --verify %TAG%^^^^{commit}
(four hats).
cat .git/refs/tags/*
or cat .git/packed-refs
(answer) depending on whether or not the tag is local or fetched from remote.
Just use git show <tag>
However, it also dumps commit diffs. To omit those diffs, use git log -1 <tag>
. (Thanks to @DolphinDream and @demisx !)
This will get you the current SHA1 hash
Abbreviated Commit Hash
git show <tag> --format="%h" --> 42e646e
Commit Hash
git show <tag> --format="%H" --> 42e646ea3483e156c58cf68925545fffaf4fb280
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).
From git mailing list, here is the way to get the list of commit hashes for tags with automatic dereferencing for annotated tags:
git for-each-ref --format='%(if)%(*objectname)%(then)%(*objectname)%(else)%(objectname)%(end) %(refname)' refs/tags
So I have a load of release folders, where those folders may be checked out from one of a few different repos, and may be dev, qa or master branches or may be production releases, checked out from a tag, and the tag may be annotated or not. I have a script that will look at the target folder and get be back an answer in the form -. The problem is different versions of git return different status' for a tag checkout.
So I found git show-ref --tags
worked initially, except for the annotated tags. However adding -d added a separate entry to the list of tags, one for the tag, the other for the annotation (the annotation commit was identified as ^{} which I stripped out with sed).
So this is the core of my script, for anyone that wants it:-
REPO=`git --git-dir=${TARGET} remote show origin -n | \
grep "Fetch URL:" | \
sed -E "s/^.*\/(.*)$/\1/" | \
sed "s/.git$//"`
TAG=`git --git-dir=${TARGET} show-ref -d --tags | \
grep \`git --git-dir=${TARGET} show --quiet --format=format:%H HEAD\` | \
cut -d\ -f2 | \
cut -d/ -f3 | \
sed "s/\^{}$//"`
if [ "${TAG}" == "" ] ; then
BRANCH=`git --git-dir=${TARGET} show-ref --heads | \
grep \`git --git-dir=${TARGET} show --quiet --format=format:%H HEAD\` | \
cut -d\ -f2 | \
cut -d/ -f3`
TAG=${BRANCH}
fi
Source: Stackoverflow.com