Get the commit hash for a tag

后端 未结 5 1202
感动是毒
感动是毒 2021-01-01 18:05

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 commi

相关标签:
5条回答
  • 2021-01-01 18:30

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

    0 讨论(0)
  • 2021-01-01 18:32

    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.

    0 讨论(0)
  • 2021-01-01 18:41
    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

    0 讨论(0)
  • 2021-01-01 18:41

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

    0 讨论(0)
  • 2021-01-01 18:47

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

    EDIT:

    Actually a better solution would be :

    git show-ref -s mylabel
    

    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:

    • git show-ref
    0 讨论(0)
提交回复
热议问题