2

I know git command "git branch --contains" can show on what branch(es) a tag can be found. But the output includes all children/sub branches. For example:

master ----------------------------
          |    \
        tag_X   \
                 \
                  ------------------ branch_A
                    |      \
                   tag_Y    \
                             \
                              --------------- branch_B

When we run "git branch --contains tag_X" we will get

branch_A
branch_B
master

although tag_X was created on master, not branch_A and branch_B

Is there any command in git to show only the specific branch where the tag was initially created, excluding all children/sub branches? If there is none, any idea how to get it (parsed from log, status, etc)? Thanks!

M.Ridha
  • 483
  • 1
  • 6
  • 21

2 Answers2

0

Git doesn't store that. A tag is simply a name pointing to a commit. If you look in .git/refs/tags you will see a text file containing the commit reference for every tag.

Since several branches could share the same commit, a tag can belong to several branches. There is also no way to get that from logs because the only thing stored is the relevant commit.

Jarryd
  • 1,312
  • 11
  • 17
  • Jarryd, thanks for your response. I found that the specific branch where a tag was initially created is displayed as the first branch (at the bottom) of the "git branch --contains tag" output. The rest of the branch(es), if they exist, are just the child(ren) of that branch that inherit the same tag. – M.Ridha Feb 13 '12 at 22:53
  • I'm not sure if that works in all cases. It makes sense if you started with a branch and then made several forks; you can think of it walking down the tree of commits and when it gets to the commit, the branch it's on is output first. But what if you have two branches that are interleaved because you have merged them into each other at several points? I'll have to try it out. The other thing is in your example, if you created tag_X whilst on branch_A, it will still print master first in the list for --contains. – Jarryd Feb 13 '12 at 23:51
0

How you tried this

git branch --contains <commit>

Not sure if it works with the tag name, you may have to do a git show to get the associated hash

taken from How to list branches that contain a given commit?

Community
  • 1
  • 1
Olivier Refalo
  • 50,287
  • 22
  • 91
  • 122
  • Olivier, I found using the associated hash gives the same output as using the tag name, i.e. doesn't give the specific branch. But thanks for your response! – M.Ridha Feb 13 '12 at 22:53