8

I would like to know if it's possible in Git to retrieve a list of tags (much like the result of the git tag command), but the list should be limited only to a certain branch.

If this is possible, can anyone explain how this is done? Perhaps with some pattern-matching magic?

Wolfgang Schreurs
  • 11,779
  • 7
  • 51
  • 92

2 Answers2

6

I think this will do what you want:

 git log --pretty='%H' <branch> |
   xargs -n1 git describe --tags --exact-match 2>/dev/null

This uses git log to get a list of commits in a branch, and then passes them to git describe to see if they correspond to a tag.

larsks
  • 277,717
  • 41
  • 399
  • 399
  • 1
    +1, best way. can be made really faster by adding `--simplify-by-decoration` to `git log`, it gives only commits that match a refspec so you don't loop over *all* commits – CharlesB May 15 '12 at 15:02
  • Hey, i am also looking for a solution similar to this : I just posted a question on [link](http://stackoverflow.com/questions/11316306/listing-the-tags-in-git-from-a-specific-branch) and then happened to read your question – iDev Jul 03 '12 at 18:03
  • @iDev: Did you try the solution of larsks? Did this work out for you? – Wolfgang Schreurs Jul 05 '12 at 04:54
6

Another approach would be possible with the new git tag options --merged (in git 2.7+, Q4 2015)

git tag --merged <abranchname>

See commit 5242860, ... (10 Sept 2015) by Karthik Nayak (KarthikNayak).
(Merged by Junio C Hamano -- gitster -- in commit 8a54523, 05 Oct 2015)

tag.c: implement '--merged' and '--no-merged' options

Use 'ref-filter' APIs to implement the '--merged' and '--no-merged' options into 'tag.c'.

  • The '--merged' option lets the user to only list tags merged into the named commit.
  • The '--no-merged' option lets the user to only list tags not merged into the named commit.

If no object is provided it assumes HEAD as the object.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • This is okay, but it does not show tags in creation order, which can be an issue when multiple branches are tagged and merged. – ingyhere May 08 '18 at 17:32
  • 1
    @ingyhere Then you would need to add a sort order option: https://stackoverflow.com/a/34919313/6309: `git tag --sort=-creatordate ` – VonC May 08 '18 at 17:37