How to list all tags in a specific git branch

I have several branches in my git repository. I would like to know if there is a team that lists all the tags within a particular branch, not all the tags of the entire repo.

I tried git tag --contains . but he did not work properly.

Image 1, there is a list of all tags with their hashes (the marked two tags have the same hash / commit) enter image description here Image 2, as you can see, I am in a branch named "b" enter image description here Image 3, I asked which branch contains the hash of both tags (both have the same hash), and he said that they are on branch "b" (the one in which I am currently included) enter image description here

Image 4 describing branch tags, It only gave me one tag enter image description here

Image 5, also describing the hash tags, which should indicate a commit marked with both tags, it only shows the ONE tag again <img src = "https://i.stack.imgur.com/8D5RN.png" alt = "enter a description images here ">

+5
source share
2 answers

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 
+5
source

Use git tag --sort='creatordate' --merged to view all tags available / available from HEAD and sort them in chronological order.

You can specify ref if you do not want HEAD, for example git tag --sort='creatordate' --merged mybranch

+7
source

All Articles