Search code examples
gitbranchgit-branchgit-tag

Do git tags apply to all branches?


I'm getting my feet wet with git tagging, but my previous background is in Subversion, where "tags" were really just copies, not "real" tags...

If I add a tag to a git repo, is it applied to all branches or only the current one?

For example, if I currently have these branches (git branch -v):

* master deadbeef My master head comment
  dev    baddfeed My def head comment

And the currently checked out branch is master, as you can see. Now suppose I git tag -a TAGNAME, does TAGNAME apply only to deadbeef (master branch) or to baddfeed (dev branch) as well?

e.g., Say I subsequently switch to the dev branch (i.e., not the branch the tag was created on) before checking out the tag:

git checkout dev
git checkout TAGNAME

Do I then end up with a checkout of baddfeed or will the tag checkout (second line) switch me back to the master branch (where the tag was created) and give me a checkout of deadbeef? (Or third option, is my understanding of creating and restoring tags too flawed or too simplistic for the answer to be as simple as one of those two options?)

Also, does the answer to my question change any if I use a lightweight tag (git tag TAGNAME) instead of an annotated tag?


Solution

  • Let's say you are in branch myBranch. And you create a tag called myTag

    -----A-----B-----C-----D-----H-------I-----> master
                           \
                            \
                             \---E----F-----G-----> myBranch
                                            |
                                            myTag
    

    The tag will be only on the commit object of the myBranch branch. I've worked with CVS too, and it tags revisions too, not all branches (but in CVS branches are special tags). I don't think.

    If you checkout myTag, you will be in the commit G of the example. You can't create tags with the same name on differente branches. If you do it, you will move the tag.

    There's no difference for an annotated tag related to this.

    Note: when checking out tags, you end up with "detached HEAD" mode. Your changes will be lost unless you create another branch starting from the tag checked out.