How to find the tag associated with a given git commit?

How about this?

git tag --points-at <commit-id>

It gives you all the tags the given commit has (whereas git describe only gives you one), and does not include tags on descendant commits (like git tag --contains does).


Check the documentation for git describe. It finds the nearest tag to a given commit (that is a tag which points to an ancestor of the commit) and describes that commit in terms of the tag.

If you only want to know if the commit is pointed to by a tag then you can check the output of:

git describe --exact-match <commit-id>

If what you want is the first tag containing the commit then:

git describe --contains <commit>

gives the best answer IMO. If you have frequent tags than using "git tag --contains" on an old commit in a big repository can take a while to run and gives you all of the tags that contain that commit.

This form of git describe runs very quickly and gives you a single output value which is the first tag containing the commit and how far back your commit is.

Tags:

Git