如何在 git 中列出指向特定提交的所有标记

我已经看到了命令 git describegit-name-rev,但我还没有设法让他们列出一个以上的标记。

示例: 我有 sha148eb354,我知道标记 A 和 B 指向它。因此,我希望 git 命令 git {something} 48eb354能够产生类似于“ A,B”的输出。我对知道相对于其他标记或分支的引用只是标记的精确匹配不感兴趣。

42677 次浏览

(Edit: This answer was written long before git tag had the --points-at switch – which is what you would use nowadays.)

git for-each-ref --format='%(objectname) %(refname:short)' refs/tags/ |
grep ^$commit_id |
cut -d' ' -f2

Pity it can’t be done more easily. Another flag on git tag to include commit IDs could express that git for-each-ref invocation naturally.

The following command does the job, but directly parse the content of the .git directory and thus may break if the git repository format change.

grep -l -r -e '^48eb354' .git/refs/tags|sed -e 's,.*/,,'

git show-ref --tags -d | grep ^48eb354 | sed -e 's,.* refs/tags/,,' -e 's/\^{}//'

should work for both lightweight and annotated tags.

git tag --points-at HEAD

Shows all tags at HEAD, you can also substitute HEAD with any sha1 id.

You can use:

git tag --contains <commit>

that shows all tags at certain commit. It can be used instead of:

git tag --points-at HEAD

that is available only from 1.7.10.

For current commit you can use

git tag --points-at $(git log -n1 --pretty='%H')