Get a commit hash for a tag

Related to Creating a git show to display information in machine-parsing format , but I'm tired of the fact that I now need to do a lot of parsing to get a commit hash.

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

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

I would just print me my message # but he says

 tag mylabel Tagger: user < user@x.com > Some comment 446a52cb4aff90d0626b8232aba8c40235c16245 

I was expecting one line of output with just a commit line, but now I need to parse the last line?

+6
source share
5 answers

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

EDIT:

In fact, the best solution would be the following:

 git show-ref -s mylabel 

EDIT bis: As mentioned in the comments, be careful with annotated commits (which are native objects). For a more general solution, read @ michas answer.

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


Resources

+6
source

git help rev-parse says:

  <rev>^{}, eg 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. 

Typically, you use tag^{} to refer to this commit.

You have two different types of tags:

  • Lightweight tags simply indicate an existing commit
  • annotated tags are those objects that have a pointer to a separate commit object

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

Use the git rev-parse tag^{} to get the SHA1 litter fix.

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 tag and the SHA1 of the associated commit.

There is also a git show tag to give you detailed tag information (annotated).

+6
source

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

+3
source

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

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

-1 : it reports only print 1 commit

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

0
source

Source: https://habr.com/ru/post/946098/


All Articles