Sub topic 11
Annotated tags: tag objects
This chapter assumes you know basic Git use (init, add, commit, log), can work in a Unix-style shell, and have read the earlier chapters on blobs, trees, commits, and references. An annotated tag is the fourth Git object type. It is a stored object, not only a name.
What a tag object holds
A commit records a snapshot. A tag object records a label for another object. It stores five pieces of information:
- the ID of the target object
- the type of the target (usually
commit) - the tag name
- the tagger, with a timestamp
- a message
Because a tag is an object, it has its own ID, computed the same way as for every other object: a hash of the header and the content. A ref named refs/tags/v1.0 then points at that tag object. The tag object points at the commit.
Creating and reading a tag
Run this inside a repository that already has at least one commit. The -a option creates an annotated tag, and -m supplies the message.
git tag v0.1
git tag -a v1.0 -m 'First release'
git cat-file -t v0.1
git cat-file -t v1.0
git cat-file -p v1.0
git rev-parse 'v1.0^{commit}'The two cat-file -t commands print different types. v0.1 prints commit, because a plain git tag only creates a ref that points straight at the commit. v1.0 prints tag, because the ref points at a new tag object.
The -p output for v1.0 looks like this. The ID, name, and date will differ in your repository.
object 3f2a9c1d5e7b8a4c6d0e1f2a3b4c5d6e7f8a9b0c
type commit
tag v1.0
tagger Ada Lovelace <[email protected]> 1764862033 +0000
First releaseThe headers come first, then a blank line, then the message. This layout is the same one commits use.
Peeling a tag to its target
Tools often need the commit, not the tag object. The suffix ^{commit} follows the tag to the commit it points to. This is called peeling. In the last command above, git rev-parse 'v1.0^{commit}' prints the commit ID. Without the suffix, git rev-parse v1.0 prints the ID of the tag object itself. Quote the argument, because some shells treat the braces specially.
Why use annotated tags
Annotated tags carry the tagger and a message, so they record who marked a release and why. They can also be signed with GPG or SSH (git tag -s), which lets others verify the label. Commands such as git describe use annotated tags by default and ignore lightweight ones unless you pass --tags. For releases, annotated tags are the usual choice.
Common errors
- Reading the wrong ID.
git rev-parse v1.0returns the tag object ID. It does not equal the commit ID. Use^{commit}when you need the commit. - Missing identity. Creating an annotated tag needs a configured
user.nameanduser.email. Without them, Git may refuse or guess an identity from the system. - Expecting tags to move. A tag object never changes. To "move" a tag you must delete the ref and create a new tag object, and anyone who already fetched the old tag keeps it.
- Forgetting to push.
git pushdoes not send tags by default. Push one withgit push origin v1.0.
Exercise
You run git rev-parse v1.0 and git rev-parse 'v1.0^{commit}' in a repository where v1.0 is an annotated tag. The two IDs differ. Why, and what would you see if v1.0 were a lightweight tag?
Show answer
The first command prints the ID of the tag object, because the ref refs/tags/v1.0 points at it. The second peels the tag and prints the ID of the commit it targets. For a lightweight tag there is no tag object, since the ref points straight at the commit, so both commands would print the same commit ID.
Exercise
Which command prints the target, type, tag name, tagger, and message of the annotated tag v1.0?
Show answer
git cat-file -p v1.0