Muzzlet
Muzzlet

Reputation: 239

Will git tag automatically move to main branch when merged?

I have created a git tag in the dev branch. But if I merge it from dev to main branch, will the git tag be automatically be added in the main as well?

Upvotes: 2

Views: 2365

Answers (2)

matt
matt

Reputation: 535616

Possible misconception #1:

Tags do not move. A tag is attached to a particular commit. If you don't somehow destroy that commit (which is not an easy thing to do), and if you don't delete the tag, the tag will remain attached to that commit, forever. Indeed, as long as you don't delete the tag, the commit cannot be destroyed; that is one of the functions of a tag.

Possible misconception #2:

Commits are not "on" branches. A branch names a single commit, and that is all it applies to. If you (and Git) can trace from a given commit backwards through the parent chain to some other commit, such as the one your tag is attached to, then we can say the second commit is reachable from the first.

Conclusion:

I presume you mean that you attached a tag to a commit that is reachable from dev. Thus the tagged commit, and therefore the tag, is reachable from dev. If you then merge dev into main using a true merge, then, by definition of what a merge is, the tagged commit, and therefore the tag, will now also be reachable from main.

But if you do not use a true merge — if, for example, you rebase or squash — then that relationship breaks down because all new commits are created and made reachable from main, and (obviously) none of those new commits can possibly be the tagged commit.

Upvotes: 7

mfnx
mfnx

Reputation: 3018

You cannot create a tag in a branch.

Think of a tag as a commit. A commit is a snapshot of your code at a particular time. It does not depend on any branch, it does not even belong to a branch. That is, if you delete a branch, you are not deleting any commits (fortunately).

A branch is nothing more than a pointer. If you commit to a branch several times, the pointer remembers the previous commits on that branch, which is merely a (very practical) feature. But it's nothing more than a pointer.

If you merge 2 branches, what you really are doing is merging the code of both branches into another commit, the so-called merge commit. If your tag is a commit on one of those branches, the code of the tag commit (or at least a merged version) will be in the merge commit.

To make things clearer, say you have a branch and you make a tag "in" that branch. Then you delete the branch. The tag will still be there, since that commit will not be deleted.

Upvotes: 1

Related Questions