Sub topic 12
Annotated tags as objects
This chapter assumes you finished the chapter on building a commit by hand. You should know that a ref is a file holding a hash, and that main points at a commit. You also need user.name and user.email set, because annotated tags record a tagger.
Two kinds of tag
Git has two kinds of tag, and they are stored differently.
- A lightweight tag is only a ref. The file
.git/refs/tags/<name>holds the hash of a commit, just like a branch does, except that it never moves. - An annotated tag is a fourth object type,
tag. The ref points at the tag object, and the tag object points at the target.
Because the annotated tag is an object, it can carry its own message, a tagger identity, a timestamp, and optionally a signature.
Creating and inspecting an annotated tag
Run this in the repository from the previous chapter, where main has two commits.
git tag -a v1 -m 'first release'
git cat-file -t v1
git cat-file -p v1
git rev-parse v1 'v1^{commit}'git cat-file -t v1 prints tag, not commit. The -p output looks like this, with your own hashes and identity:
object 3f1c0e5a9b7d2c4e8a6f1b0d9c8e7a6b5d4c3b2a
type commit
tag v1
tagger Ada Lovelace <[email protected]> 1796000000 +0000
first releaseThe header lines say what the tag points at (object), the type of that target (type), the tag's name (tag), and who created it and when (tagger). A blank line separates them from the message. This is the same layout as a commit object.
git rev-parse prints two different hashes. v1 resolves to the tag object itself. The suffix ^{commit} tells Git to follow the tag until it reaches a commit, so v1^{commit} gives the hash of the commit you tagged.
Comparing with a lightweight tag
You can create a lightweight tag with plumbing alone, using update-ref:
git update-ref refs/tags/v1-light HEAD
git cat-file -t v1-light
git cat-file -t v1The first cat-file prints commit, because the ref points straight at a commit. The second prints tag. The ref file .git/refs/tags/v1 contains the tag object's hash, not the commit's.
Why use an annotated tag
- It records who tagged and when, which is separate from who wrote the commit.
- It holds a message, such as release notes.
- It can be signed with
git tag -s. The signature sits inside the tag object. git describeignores lightweight tags by default, so annotated tags suit release markers.
A tag object can point at any object type, not just commits. Git's own repository tags a public key blob this way. Tags can also point at other tags, which is why peeling may take more than one step.
Common errors
fatal: no tag message?or an editor opening unexpectedly:-aneeds a message. Pass-m.Please tell me who you are: setuser.nameanduser.email, because the tagger line needs them.- Treating
v1andv1^{commit}as the same hash: they differ for annotated tags. Scripts that need the commit should use^{commit}.
Exercise
You run git cat-file -t release and see commit. Then you run git cat-file -t v1 and see tag. What does this tell you about how each tag was made, and which command do you use to get the commit hash from v1?
Show answer
release is a lightweight tag: its ref points directly at a commit. v1 is an annotated tag: its ref points at a tag object that in turn points at the commit. Use git rev-parse 'v1^{commit}' to get the commit hash.