Konstantin Shemyak
Konstantin Shemyak

Reputation: 2537

Git: find all tags, reachable from a commit

How can I list all tags, reachable from a given commit?

For all branches, it is git branch --all --merged <commit>. For most recent tag, it is git describe.

Man page git-tag suggests git tag -l --contains <commit> *, but this command does not show any of the tags which I know are reachable.

Upvotes: 6

Views: 2226

Answers (3)

Kyle Strand
Kyle Strand

Reputation: 16509

Git now supports this natively, but (perhaps unsurprisingly) in a counter-intuitive way:

git tag --merged [revision]

The behavior (and the rationale for the name) comes from git branch --merged: you are asking to see all the items that have been "merged into" the specified revision. This phrasing makes sense for branches, but unfortunately less sense for tags.

See also https://stackoverflow.com/a/41175150/1858225.

Upvotes: 5

Parker Coates
Parker Coates

Reputation: 9428

Man page git-tag suggests git tag -l --contains *, but this command does not show any of the tags which I know are reachable.

git tag --contains is for the opposite search. It shows all tags containing the given commit. (This is the same behaviour as git branch --contains.)

Upvotes: 1

CodeWizard
CodeWizard

Reputation: 142572

use this script to print out all the tags that are in the given branch

git log --decorate=full --simplify-by-decoration --pretty=oneline HEAD | \
sed -r -e 's#^[^\(]*\(([^\)]*)\).*$#\1#' \
-e 's#,#\n#g' | \
grep 'tag:' | \
sed -r -e 's#[[:space:]]*tag:[[:space:]]*##'

The script is simply a 1 long line breaked down to fit in the post window.

Explanation:
git log 

// Print out the full ref name 
--decorate=full 

// Select all the commits that are referred by some branch or tag
// 
// Basically its the data you are looking for
//
--simplify-by-decoration

// print each commit as single line
--pretty=oneline

// start from the current commit
HEAD

// The rest of the script are unix command to print the results in a nice   
// way, extracting the tag from the output line generated by the 
// --decorate=full flag.

Upvotes: 9

Related Questions