Reputation: 36343
I'm in a branch now and I am about to create a tag. Creating a tag will save the state of ALL of my branches correct? But later if I want to come back to this tag, how do I check it out with a specific branch? Because if I checkout a tag which contains state for all of my branches, how do I actually go to a specific branch within that tag? I don't want to create a NEW branch with
git checkout -b branch_name tag_name
I want to checkout an EXISTING branch on this tag when I come back to it later. I would imagine it would be something like
git checkout branch_name tag_name
but I can't find an example of that command anywhere.
Upvotes: 1
Views: 3570
Reputation: 487775
As matt and Bauhaus already noted in comments, a tag is just a name for an SHA-1.
But in fact, so is a branch!
When you do:
git checkout <sha-1>
git takes you "off" a branch and to a particular commit. Git calls this a "detached HEAD".
If you name a commit SHA-1 by some other method, such as a tag or a "remote branch" (origin/master
and the like), git does the same thing.
When you do:
git checkout branch_name
there is only one difference, but it's a very important one: git still checks out the SHA-1, just as with any other method, but it also puts you "on" the branch.
What this really means is that git writes the name of the branch into a file.1 That's what "being on a branch" means: that there's a file containing the name of the branch you are on.
Being "on a branch" mainly affects new commits. Any new commit you make, git writes the commit into the repository, along with the commit-message, author, timestamp, parent commits, and so on. Then, if you are "on a branch", git writes the new commit-ID into a file that keeps track of the tip of that branch.2 That causes the branch itself to point to the new commit. Let's say, just for example, that the new commit has SHA-1 ID c0ffee...
. Git writes c0ffee...
into the branch file.
Suppose you then git checkout master
and get "on branch master". Git switches to some other commit, on branch master
. Later, you want to come back and work more on branch branch_name
, so you git checkout branch_name
again. Git does the same thing every time: it sees that this is a branch name, looks up the SHA-1 for branch_name
, checks out commit c0ffee...
, and then—because it's a branch name—updates the "which branch am I on" file.
If you now make another new commit, git writes the commit as usual, and writes the ID—let's say it happens to be SHA-1 feedcab...
—into the branch file as usual. So you're still "on branch branch_name
", but now that means "commit feedcab...
".
The key difference here between a tag and a branch is that a tag is not expected to move, and git won't update it with new SHA-1 IDs; but a branch is expected to move, and git will update it automatically.
1The file is .git/HEAD
and after this it contains the string ref: refs/heads/branch_name
.
2Specifically, .git/refs/heads/branch_name
in this case. However, git also has "packed" refs which are stored in other files.
Upvotes: 3