Search code examples
gittagginggit-tag

Is there a way to get a tag from a specific commit in git?


How do I get the git tag(s) on a specific git commit SHA?


Solution

  • Use the --contains param.

    Example: To see what tags are on the SHA abcd1234, from the shell:

    git tag --contains abcd1234
    

    which returns the name of the tag

    v1.1
    

    If there are multiple tags on that specific commit, you'll get them back separated by newlines:

    v1.1
    LATEST_STABLE
    MY-TAG
    

    To set this in an environment variable via a bash script:

    export GIT_TAG=`git tag --contains $MY_SHA`