244

I'm currently working with a repository that has multiple branches.

When I create a tag, does that tag refer to the then-current branch?

In other words: Whenever I create a tag, do I need to switch to the desired branch and tag inside that branch so that the tag refers to that branch at that point in time?

9 Answers 9

317

CharlesB's answer and helmbert's answer are both helpful, but it took me a while to understand them. Here's another way of putting it:

  • A tag is a pointer to a commit, and commits exist independently of branches.
    • It is important to understand that tags have no direct relationship with branches - they only ever identify a commit.
      • That commit can be pointed to from any number of branches - i.e., it can be part of the history of any number of branches - including none.
    • Therefore, running git show <tag> to see a tag's details contains no reference to any branches, only the ID of the commit that the tag points to.
      • (Commit IDs (a.k.a. object names or SHA-1 IDs) are 40-character strings composed of hex. digits that are hashes over the contents of a commit; e.g.: 6f6b5997506d48fc6267b0b60c3f0261b6afe7a2)
  • Branches come into play only indirectly:
    • At the time of creating a tag, by implying the commit that the tag will point to:
      • Not specifying a target for a tag defaults to the current branch's most recent commit (a.k.a. HEAD); e.g.:
        • git tag v0.1.0 # tags HEAD of *current* branch
      • Specifying a branch name as the tag target defaults to that branch's most recent commit; e.g.:
        • git tag v0.1.0 develop # tags HEAD of 'develop' branch
      • (As others have noted, you can also specify a commit ID explicitly as the tag's target.)
    • When using git describe to describe the current branch:
      • git describe [--tags] describes the current branch in terms of the commits since the most recent [possibly lightweight] tag in this branch's history.
      • Thus, the tag referenced by git describe may NOT reflect the most recently created tag overall.
3
  • So, for a given tag (e.g., v0.1.0), to know what actual source is in an actual build (of that source), you really need to know the branch the build was based on, right? I.e., a given commit may have different descendants in different branches, right? Apr 11, 2018 at 18:14
  • 2
    @HawkeyeParker: I'm not immersed in this anymore, but you shouldn't need to know the branch, because a tag identifies a specific commit (which can be part of multiple branches), and you can investigate from there.
    – mklement0
    Apr 11, 2018 at 18:37
  • But if I do git show [tagname] then it shows a branch name above Author and Date, which contradicts "running git show <tag> to see a tag's details contains no reference to any branches" Jun 19, 2019 at 13:38
207

If you create a tag by e.g.

git tag v1.0

the tag will refer to the most recent commit of the branch you are currently on. You can change branch and create a tag there.

You can also just refer to the other branch while tagging,

git tag v1.0 name_of_other_branch

which will create the tag to the most recent commit of the other branch.

Or you can just put the tag anywhere, no matter which branch, by directly referencing to the SHA1 of some commit

git tag v1.0 <sha1>
58

Tags and branch are completely unrelated, since tags refer to a specific commit, and branch is a moving reference to the last commit of a history. Branches go, tags stay.

So when you tag a commit, git doesn't care which commit or branch is checked out, if you provide him the SHA1 of what you want to tag.

I can even tag by refering to a branch (it will then tag the tip of the branch), and later say that the branch's tip is elsewhere (with git reset --hard for example), or delete the branch. The tag I created however won't move.

1
  • 37
    In other words, tags are just nice names for the ugly git hashes. The tag (and the hash) exists no matter which branches exist(ed).
    – C-Otto
    Jan 30, 2013 at 20:58
11

When calling just git tag <TAGNAME> without any additional parameters, Git will create a new tag from your current HEAD (i.e. the HEAD of your current branch). When adding additional commits into this branch, the branch HEAD will keep up with those new commits, while the tag always refers to the same commit.

When calling git tag <TAGNAME> <COMMIT> you can even specify which commit to use for creating the tag.

Regardless, a tag is still simply a "pointer" to a certain commit (not a branch).

7

We can create a tag for some past commit:

git tag [tag_name] [reference_of_commit]

eg:

git tag v1.0 5fcdb03
4

If you want to tag the branch you are in, then type:

git tag <tag>

and push the branch with:

git push origin --tags
1

If you want to create a tag from a branch which is something like release/yourbranch etc Then you should use something like

git tag YOUR_TAG_VERSION_OR_NAME origin/release/yourbranch

After creating proper tag if you wish to push the tag to remote then use the command

git push origin YOUR_TAG_VERSION_OR_NAME
1

A related and useful command is

git branch --contains tag/<tag>

which will give you a list of all the branches a provided tag is on

more detail here…

1

No, a git tag will apply to all branches that contain the commit that was tagged. It does not matter what branch you were in at the time of tagging.

Here's an example:

  • tag-a will appear for both branch1 and branch2, even though it was tagged "from" branch1.
  • tag-b will appear in branch1 only, it will not appear in branch2
  • tag-c will appear in branch2 only, it will not appear in branch1
a---b---> branch1
 \
  --c---> branch2

Script to reproduce example:

git switch -c branch1
echo a > a.txt; git add a.txt; git commit -m "a.txt"
git tag tag-a

git switch -c branch2
echo c > c.txt; git add c.txt; git commit -m "c.txt"
git tag tag-c

git switch branch1
echo b > b.txt; git add b.txt; git commit -m "b.txt"
git tag tag-b

git switch branch1
git log --oneline --graph
* e564800 (HEAD -> branch1, tag: tag-b) b.txt
* 5c82b05 (tag: tag-a) a.txt

git switch branch2
git log --oneline --graph
* 67c3177 (HEAD -> branch2, tag: tag-c) c.txt
* 5c82b05 (tag: tag-a) a.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.