• Servers and Tools


From the class:  Inside Git

There's another kind of reference called a tag. It's similar to a branch or to a head reference. It points to a particular commit.

And the idea here is we want some kind of a name that points to a commit that doesn't change over time. So it's really useful for things like deployment where you want to say that this particular commit is version one or version two of the system. It's like a label that just doesn't move.

There's two types of tags, a simple one and an annotated one. The simple one is just a file that will get created in the Tags folder that points to a particular sha1 hash. So if I say git log, and we'll just take a look at one-liners-- not online, one line-- we could create a tag, for instance, that says that this commit here is version one of the system. So to do that, I can say git update ref refs/tags/v1, maybe 1.0. And that's going to point to eb41b.

And now let's take a look at what that file actually contains. So .git/refs/tags/v1.0, and it's just literally the contents of the sha1 hash. So it's going to point to this commit here. And we can treat tags just like other references. So we could checkout this tag if we wanted to, and that would checkout this commit to our working directory.

There's another kind of tag we can store called an annotated tag, which will create an actual Git object in the Objects database. To create one of those, we can use the tag command and provide it this A option, which means we're going to create this heavier weight tag. Then I'll provide it the name of the tag, so in this case, I'll call it v1.1, and the abbreviated sha1 hash of the thing that I want it to point to, in this case, an actual commit.

Then I can provide an M option which let's me type a message to associate with the tag. You might say this is my first annotated tag. I can even sign this tag if you wanted to provide your signature for the tag to verify a deployment, for instance.

Once I do that, a new reference file is going to be put into the .git/refs/tags folder called v1.1. And if I take a look at it, the contents of that file is going to be another sha1 hash, but this time it's going to point to a special new object in our database called a tag. So we haven't seen that yet, but there's an object type called a tag, and it's going to be in that list of object files that are in the database.

Let's take a look at it using the git show command or they git cat-file command so we can see the low level version of it. And I'll use the pretty option, and we'll provide it the sha1 of f35d. That should be good enough.

And this is what a tag object looks like. It provides the object that the tag points to, and this hash here is the commit, up here. This points to this commit.

And then it says what type is the object that it points to; the name for the tag, the friendly name; and then the tagger, my name with my email address and the time that I made the tag; and then a special message here. You might also see a signature here if I actually signed the tag. So this is really useful for creating tags around deployments of versions of the software.

This is called a heavier weight tag because it actually creates that object in the database. And we can find it by looking in the Objects folder again and just searching for the one that has the hashtag that we just used here. Let's see, f35d. Now this is getting a little hard to find things, but, let's see, f35d. Here it is. This is an actual object, in this object type is a tag that contains all this information here in its contents.