Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When I checkout a branch in GIT and then tag without explicity specifying the branch name what happens?

Tags:

git

branch

tags

A very simple question I hope... but deceptively difficult to get a straight answer from the documentation or playing around on the command line in GIT. Here is the scenario, I have a local repo and am in master. I create a branch called branch1, I checkout branch1. I then make some change, add / commit the changes (staging and commit). At this point I tag branch1 with "git tag 1.0".

My question is - does that tag automatically pick up the fact that I am in branch1 and therefore apply only to branch1. Or do I need to use the command "git tag 1.0 branch1" explicitly naming the branch to which it refers. I tried "git branch --list" etc from both within branch1 working directory and master working directory and they both list the tag in either case. If I rebased branch1 back into master then I would not be surprised by this outcome. But for time being, without merging changes back, how can I ask to see just the tag that refers to a given branch on the command line (if that is the case)?

like image 878
arcseldon Avatar asked Nov 20 '25 12:11

arcseldon


2 Answers

Tags don't refer to branches. They refer to commits. It's completely irrelevant what branch you're on. You might not even be on a branch at all.

git tag foo branch1

Doesn't apply the tag to branch1. It's shorthand notation for applying it to the HEAD commit of branch1.

like image 132
Jörg W Mittag Avatar answered Nov 23 '25 13:11

Jörg W Mittag


Tags don't apply to branches, they apply to commits. Tags are an additional label to identify a single commit. When you switch to master, that commit still has that tag, so it's no surprise to see it in the list.

Additionally, branches are also just a label for one commit. When you check out a branch, you are making the named commit the HEAD of your current repository.

like image 25
Femaref Avatar answered Nov 23 '25 13:11

Femaref



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!