27

It is related to Making git show to show information in a machine parseable format but I am getting tired of the fact that I now have to do a lot of parsing to get the commit hash.

Can someone give me a command that will print the commit hash (and only the commit hash) tag for a given git tag? I was hoping

git show mylabel --pretty=format:"%H" --quiet 

Would just print me my commit # but it says

tag mylabel Tagger: user <[email protected]> Some comment 446a52cb4aff90d0626b8232aba8c40235c16245 

I was expecting one line of output with just the commit line but I now have to parse for the last line?

4
  • 1
    I think @michas' solution should be the accepted answer Commented Jun 24, 2016 at 22:03
  • 2
    Possible duplicate of How to tell which commit a tag points to in Git? Commented Jun 22, 2019 at 12:58
  • What I can't understand is that if I use git log --color=never I get a full log of commits with branches and tags listed next to the commit hash, but then, if I want to filter the first lines to view all hashes next to their tag and branch names, as you do, and I pipe it through grep, suddenly git log --color=never | grep '^commit' simplifies it's output and the commit lines no longer have the tag and branch names included. The same happens if I just redirect to a file. The output is missing the tag name embellishments. Commented Jun 10 at 1:34
  • Ok, I got it from man git log. The "embellishments" are officially known as "decorations" and are implicitly provided to terminals but are dropped when piping. They can be forced with the addition of --decorate. So git log --decorate --color=never | grep '^commit.*tag' lives a list of all commit hashes that are also tagged. Commented Jun 10 at 1:51

5 Answers 5

31

What about git log -1 --format=format:"%H" mylabel

EDIT:

Actually a better solution would be :

git show-ref -s mylabel 

WARNING This only works for Unannotated tags For a more general and safer command see https://stackoverflow.com/a/1862542/1586965

EDIT bis: As mentioned in the comments, be careful with annotated commits (which are objects of their own). To have a more generic solution, read @michas answer.

You can see the difference when you do a git show-ref -d mylabel.


Resources:

Sign up to request clarification or add additional context in comments.

4 Comments

Be aware that this does NOT work for annotated tags, for which the tag is different from its commit.
The git log -1 is an useful option since I can play around with the format. However the git log command gives me the commit hash and commit name not the TAG's hash and the tagger. Is there any way I can find that using git-log?
This is going to show multiple things if there are also remote branches labeled mylabel
(which would interfere with scripting)
22

git help rev-parse says:

 <rev>^{}, e.g. v0.99.8^{} A suffix ^ followed by an empty brace pair means the object could be a tag, and dereference the tag recursively until a non-tag object is found. 

Generally you use tag^{} to refer to that commit.

You have two different kind of tags:

  • lightweight tags are just pointers to an existing commit
  • annotated tags are objects on there own which contain a pointer to a separate commit object

Use git rev-parse tag to get the SHA1 of the tag itself.

Use git rev-parse tag^{} to get the SHA1 of the underlaying commit.

For lightweight tags both are the same. For annotated tags they are not.

You can also use git show-ref -d tag, which will show you both the SHA1 of the tag and the SHA1 of the associated commit.

There is also git show tag to give you details about an (annotated) tag.

1 Comment

This is helpful, and works. It would be good to get a bit more depth on the possible structures that can arise with tags and annotated tags. Presumably a simple tag does not have it's own separate hash, but an annotated tag, does, and so the pointer needs to be followed first, but can a tag point to another tag hash and not a genuine commit?
6

git rev-parse mylabel^{} should do what you want. See man gitrevisions for more info on ^{} and other operators.

Comments

5
git log <tag or branch> -1 --pretty=%H 

-1: tells it to only print 1 commit

--pretty=%H: tells it to only print the full hash

Comments

-1

just a guess: try "git show --pretty=format:"%H" --quiet mylabel"

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.