To print all tags that point to a specific commit, you can do:
git for-each-ref refs/tags | grep HASH
Or if you are on Windows and not using Cygwin or the like:
git for-each-ref refs/tags | find "HASH"
If you want only the tag name, you cannot use Git --format , because we need it for grep'ing. Thus, we need to remove material that we are not interested in at the last stage (Linux / Cygwin):
git for-each-ref refs/tags | grep HASH | sed -r "s/.*refs\/tags\/(.*)/\1/"
Regarding the initial question:
This repeats across all tags in all branches, asks Git which branches each tag is and filters based on the specified branch name, but be careful, super slow :
git for-each-ref refs/tags --format "%(refname)" | while read x do if git branch --contains $x | grep -q "^[ *] master$" then echo $x fi done
The following taken from another answer is much faster:
git log --simplify-by-decoration --decorate --pretty=oneline "master" | fgrep 'tag: '
... but if several tags point to the same commit, it will produce:
(HEAD, tag: Test-Tag1, tag: Test-Tag2, Test-Tag3, fork/devel, devel) (tag: Another-Tag) (tag: And-Another)
(three Test-Tag* tags pointing to the same commit)
I wrote a Python script that outputs only tags, one per line (tested only on Windows):
import os from subprocess import call print("-" * 80) dirpath = r"D:\Projekte\arangodb" # << Put your repo root path here! tagdir = os.path.join(dirpath, ".git", "refs", "tags") commitspath = os.path.join(dirpath, "_commit_list.tmp") # could probably read from stdin directly somewhow instead of writing to file... # write commits in local master branch to file os.chdir(dirpath) os.system("git rev-list refs/heads/master > " + commitspath) tags = {} for tagfile in os.listdir(tagdir): with open(os.path.join(tagdir, tagfile), "r") as file: tags[file.read().strip()] = tagfile tags_set = set(tags) commits = {} with open(commitspath, "r") as file: for line in file: commits[line.strip()] = 1 os.remove(commitspath) commits_set = set(commits) for commit in sorted(commits_set.intersection(tags_set), key=lambda x: tags[x]): print(tags[commit])
Result:
Test-Tag1 Test-Tag2 Test-Tag3 Another-Tag And-Another
The commit hash code can be optionally printed for each tag, just change the last line to print(commit, tags[commit]) . By the way, the script works very well!
Ideally, Git will support something like the following command to avoid all these workarounds:
git tag --list --branch master