Git: How to find out on which branch a tag is?

GitTagsBranch

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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

Solution 3 - Git

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.

Solution 4 - Git

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

Solution 5 - Git

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

Solution 6 - Git

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 --pretty='%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.

Solution 7 - Git

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.

https://stackoverflow.com/questions/5703237/git-is-there-something-like-per-branch-tags<br /> http://git-scm.com/book/en/Git-Basics-Tagging

Here is a good explanation.

Solution 8 - Git

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/.*\///"

Solution 9 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionViacheslav KondratiukView Question on Stackoverflow
Solution 1 - GitVonCView Answer on Stackoverflow
Solution 2 - Gitdavid1977View Answer on Stackoverflow
Solution 3 - GitarpiebView Answer on Stackoverflow
Solution 4 - GitPeter JaricView Answer on Stackoverflow
Solution 5 - GitSwagsView Answer on Stackoverflow
Solution 6 - GitPyr3zView Answer on Stackoverflow
Solution 7 - GitRené HöhleView Answer on Stackoverflow
Solution 8 - GitttfreemanView Answer on Stackoverflow
Solution 9 - GitJhonny Ramirez ZeballosView Answer on Stackoverflow