I have the SHA ID of a commit that I am interested in and would like to know how to find the first tag that contains it.
Asked
Active
Viewed 1.3k times
30
-
Does this answer your question? http://stackoverflow.com/questions/1474115/find-tag-information-for-a-given-commit – jasonwryan Sep 09 '12 at 23:55
-
Related: https://stackoverflow.com/questions/7923091/how-to-list-all-tags-that-contain-a-commit – Ciro Santilli OurBigBook.com May 31 '18 at 16:31
2 Answers
26
As previously stated, this can be done with git describe
. In your particular case, however, you may find it more convenient to run git name-rev --tags --name-only <SHA>
, which outputs exactly what you want. See git-name-rev(1).

user3188445
- 5,257
17
git describe --contains "$committish"
shows a reference to the commit built on a tag plus a ~$n
ancestorhood count, so the following command shows the most recent tag that contains a commit:
git describe --contains "$committish" | sed 's/~.*//'
If there is no tag that contains this commit, git describe
will fail. If you'd like to get the (abbreviated) committish instead, add the --always
option.

Stéphane Chazelas
- 544,893

Gilles 'SO- stop being evil'
- 829,060