Sub topic 12
Annotated versus lightweight tags
This chapter assumes you know how commits point to trees and how a ref such as a branch names a commit. It also assumes you have read the previous chapter on tag objects. Here you compare the two kinds of tag and learn when to pick each.
Two kinds of tag, one namespace
Both kinds of tag live under refs/tags/. The difference is what the ref points to.
- A lightweight tag is only a ref. It holds the ID of a commit, like a branch that never moves.
- An annotated tag is a ref that holds the ID of a tag object. The tag object is stored in
.git/objectsand records the target, its type, the tag name, the tagger, and a message.
Seeing the difference
Run this in a repository that has at least one commit and a configured user.name and user.email. Git needs them to fill in the tagger line.
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 first cat-file -t prints commit, because v0.1 resolves directly to a commit. The second prints tag. The -p output for v1.0 has this shape:
object 3f2a...
type commit
tag v1.0
tagger Ada Lovelace <[email protected]> 1796000000 +0000
First releaseThe object ID, name, and timestamp will differ on your machine. The ^{commit} suffix peels the tag. Git follows the tag object to whatever it points at and stops when it reaches a commit. Without the suffix, git rev-parse v1.0 prints the ID of the tag object itself.
Comparing them
| Property | Lightweight | Annotated |
|---|---|---|
| Creates a new object | No | Yes, a tag object |
| Stores tagger and date | No | Yes |
| Stores a message | No | Yes |
| Can be signed | No | Yes, with `-s` |
| Resolves to | The commit directly | The tag object, then the target |
An annotated tag can point at any object type, not only a commit. Tagging a commit is by far the most common use.
Common errors
- Assuming
git rev-parse v1.0gives a commit ID. It gives the tag object's ID for an annotated tag. Scripts that need the commit should usev1.0^{commit}. - Expecting the tag message to change by moving the tag. Tag objects are immutable like all objects. To retag, you create a new tag object and repoint the ref, for example with
git tag -f -a. - Forgetting that tools treat the two kinds differently. By default
git describeconsiders only annotated tags.git push --follow-tagspushes only annotated tags that point into the history being pushed. - Getting an editor prompt or an identity error.
git tag -awithout-mopens your editor. Without a configured identity it fails.
Choosing between them
Ask whether the tag is a published statement or a personal marker. A release such as v1.0 is a statement, so it should carry a tagger, a date, a message, and ideally a signature. A temporary marker such as before-refactor needs none of that, and a lightweight tag costs nothing to create or delete.
Exercise
You run git tag v2.0 and git tag -a v2.1 -m 'Release' on the same commit. What does git cat-file -t print for each tag, and which command gives the commit ID for v2.1?
Show answer
git cat-file -t v2.0 prints commit, because the lightweight tag points straight at the commit. git cat-file -t v2.1 prints tag, because the annotated tag points at a tag object. git rev-parse 'v2.1^{commit}' peels the tag object and prints the commit ID.