135

I'm currently busy with a project with a lot of branches and I have a tag for last changes which where done on one of the branches. But it's not clear for me on which branch this tag is.

How to find out on which branch a tag is?

u32i64
  • 2,384
  • 3
  • 22
  • 36
Viacheslav Kondratiuk
  • 8,493
  • 9
  • 49
  • 81

9 Answers9

172

Even shorter:

git branch --contains tags/<tag>

(it works for any tree-ish reference)


If you can find which commit a tag refers to:

 git rev-parse --verify tags/<tag>^{commit}
 # or, shorter:
 git rev-parse tags/<tag>~0

Then you can find which branch contain that commit.

git branch --contains <commit>

As commented below by user3356885, for the fetched branches (branches in remotes namespace)

git branch -a --contains tags/<tag>
git branch -a --contains <commit>

As noted in Pyr3z's answer, for each candidate tag listed above, you can add:

git log -1 --pretty='%D' TAG

That will show the branches associated to that tag.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 7
    On my version of Git, 1.7.1, I can simply do `git branch --contains `. – Dan Moulding Apr 04 '13 at 10:29
  • @DanMoulding true, I have edited the answer to reflect that. I was initially interested on finding the commit attached to a tag. – VonC Apr 04 '13 at 10:32
  • 6
    Looking for tag that was created on remote branch won't produce any results in this case. Another words, no results will be produced for branches that do not exist locally. Option *-a* should be used for that. `git branch -a --contains `. Same will work for commits. – user3356885 May 22 '16 at 13:25
  • 2
    Unfortunately this returns multiple things: * (HEAD detached at 82dd3f0) master refs/tags/0.0.1-test-masterBr --> I want to programatically access the branch, no HEAD info or the tag itself – herm Sep 29 '17 at 15:40
  • @herm In that case, try `git branch --no-merge tags/` – VonC Sep 29 '17 at 21:18
  • @ VonC With this method I get a branch after taggin a commit on the master. – herm Oct 02 '17 at 09:18
  • @herm In that case, ask a new question with your configuration (OS, version of Git) and your current setup. – VonC Oct 02 '17 at 09:19
  • This returns `* (no branch)` for me on a freshly cloned repo. – Alejandro Cotilla Nov 05 '18 at 17:03
  • @AlejandroCotilla Maybe it is a tag not referenced by any branch. – VonC Nov 05 '18 at 20:41
  • @VonC thanks! I figured it out. I had to use `git branch -r --contains `. The way my repo is being cloned is on a detached HEAD initially, maybe that's why `-a` shows `* (no branch)`. – Alejandro Cotilla Nov 05 '18 at 20:47
  • This opens a list of tags with one marked green in same cases for me. I need to close that. I just want it to print the branch out that tag was tagged on to stdout. How do I do that? – redanimalwar Oct 18 '19 at 12:26
  • @redanimalwar What command do you type? On which OS? With which version of Git? In which shell/CMD? – VonC Oct 18 '19 at 12:29
  • 1
    `git branch --contains `. git version 2.23.0, zsh, open SUSE tumbleweed. I do not think this is specific to my OS or shell, I think this is the default thing modern git versions do. They open a list in a pager – redanimalwar Oct 18 '19 at 12:37
  • What I can to is `git ... | grep "*" ` that with output me `* branchname`. – redanimalwar Oct 18 '19 at 12:39
  • @redanimalwar Then those are all the branches from which the tag is accessible: that tag could have been set on that commit while being in any of those branches. – VonC Oct 18 '19 at 12:39
  • @redanimalwar indeed. Since the new `--show-current` option is not compatible with `--contain`: https://stackoverflow.com/a/55088865/6309, a grep remains a good alternative: but that references only the branch you happen to have checked out at the time of your search. – VonC Oct 18 '19 at 12:42
  • I do not really get it. "Set on that commit while beeing?" I specified a tag and not some random commit hash. Anyway I guess what you are saying it all those branches were created from the point it was tagged. That was not my question anywas. The starred marked one is the branch if was created on and I would like a simple git comment to just output me the branch cleanly without the need for anything else if possible. – redanimalwar Oct 18 '19 at 12:44
  • So its actually not what I am looking for, hmmm. But its opens a pager even if its just one branch. – redanimalwar Oct 18 '19 at 12:46
  • @redanimalwar I don't think the 'x' marks the "the branch if was created on": it only marks the branch you happen to be right now. Test it yourself: checkout another branch (ony listed by your previous `git branch --contains` command): a new `git branch --contains` of that same tag will put the '*' in front of the branch you just checked out. – VonC Oct 18 '19 at 12:46
  • @redanimalwar You can prevent the pager to bother you with `--no-pager`: https://stackoverflow.com/a/2183920/6309 – VonC Oct 18 '19 at 12:47
  • @VonC What are the differences between `git branch -a --contains tags/` and `git log -1 --pretty='%D' TAG` – John May 03 '22 at 08:13
  • @John Not much for this question. – VonC May 03 '22 at 08:30
50

If "git branch --contains " does nothing, be sure that you are including all branches, both remote and local branches:

git branch -a --contains <tag>

From the git help:

Specific git-branch actions: -a, --all list both remote-tracking and local branches

david1977
  • 739
  • 6
  • 6
9

In regards to @VonC's comment about finding the commit referenced by a tag, just use:

git show <tag>

Since a tag is tied to a specific commit, it can be used to show that commit - which will give you the full commit details.

arpieb
  • 340
  • 3
  • 7
8

My problem with the top answers here—

—specifically solutions like

git branch -a --contains TAG

and similar had the problem of being able to list multiple branches in the output, and it isn't clear which is the one the tag ACTUALLY originated in:

$  git branch --contains TAG
   branch-A
   branch-B
 * branch-C
   branch-D

(the * marks the current branch = not relevant)

Oh, and sorting with --sort=-committerdate or =-taggerdate doesn't exactly clarify the original branch, since these ref attributes can be updated by actions not related to the TAG in question.

git show TAG

DID give me the true answer to my question ("which branch was this tag created on?"), however the git show format is quite bulky by default, and what I was looking for was an efficient, machine-friendly output format in order to pass to some automated scripts.

So turns out, git log is the core command for the job:

git log -1 --format='%D' TAG

This gives output like:

tag: TAG, origin/branch-B, branch-B

Which tells us exactly the branch the tag originated on, and is much more machine-readable.

Pyr3z
  • 91
  • 1
  • 5
7
git branch --contains tag

does nothing for me, but I found my solution to this problem in git gui.

Start it like this:

git gui

(On my Ubuntu I had to install it first with sudo apt-get install git-gui.)

Then I selected the menu item Repository -> Visualize All Branch History. In the resulting window I then selected the menu item File -> List References.

Another window popped up, listing all my tags (and other references). These are clickable and after clicking one of them I just had to check the bottom left frame for the list of branches. Like this:

Parent: somesha (message)
Parent: someothersha (another message)
Child:  anothersha (yet another message)
Branches: branch1, master, remotes/origin/branch2, remotes/upstream/branch1, etc
Follows: v1.1.2
Precedes: v1.1.4
Peter Jaric
  • 5,162
  • 3
  • 30
  • 42
3

You can also try this , had similar use case and this worked for me

git ls-remote --heads origin | grep $CI_COMMIT_SHORT_SHA  | sed "s/.*\///"

Slightly different but taking inspiration from @ttfreeman's answer

David Buck
  • 3,752
  • 35
  • 31
  • 35
Swags
  • 31
  • 1
0

With a Tag you mark a reference. So when you are on a dev branch and Tag this state. Your tag is on the actual reference. So in this case you can look to gitk or another tool where the tree is shown. There you can see on which reference the Tag is.

git: Is there something like per-branch tags?
http://git-scm.com/book/en/Git-Basics-Tagging

Here is a good explanation.

Community
  • 1
  • 1
René Höhle
  • 26,716
  • 22
  • 73
  • 82
0

A tag is always referring to commit number. Using that tag number you can find the branch from which the tag was placed using this:

git for-each-ref | grep ${commit_num} | grep origin | sed "s/.*\///"
ttfreeman
  • 5,076
  • 4
  • 26
  • 33
-1

Step 1. Get commit id:

git show {tag name}

Step 2. Copy commit id and paste to get all branches:

By example:
git branch --contains 94a152c2d1c6830c5a044ecf20526d51e64bda83