Sub topic 12
Annotated Tags as Objects
This chapter assumes you have worked through the earlier chapters of this tutorial. You should be comfortable with blobs, trees, commits, refs, and git cat-file. You should also have the plumbing-demo repository with its two commits on main. A tag looks like a simple label, but Git has two kinds. One is a plain ref. The other is a fourth object type with its own hash.
Two kinds of tag
A lightweight tag is a file under .git/refs/tags/ that holds a commit hash. It is a ref, like a branch that never moves. No new object is created.
An annotated tag is a tag object stored in the object database. The file under .git/refs/tags/ holds the hash of that tag object. The tag object in turn holds the hash of the thing being tagged, plus a tagger, a date, and a message.
| Property | Lightweight tag | Annotated tag |
|---|---|---|
| Created by | `git tag v0` | `git tag -a v1 -m "..."` |
| New object in the database | No | Yes, a `tag` object |
| What `refs/tags/<name>` contains | A commit hash | A tag object hash |
| Tagger, date, message | None | Stored in the object |
| Can be signed | No | Yes |
| Used by `git describe` by default | No | Yes |
Creating an annotated tag
Run this in plumbing-demo. Like commit-tree, git tag -a needs user.name and user.email to be set, because it records a tagger.
git tag -a v1 -m 'first release'
git cat-file -t v1
git cat-file -p v1
git rev-parse v1 'v1^{commit}'The first cat-file prints tag, not commit. The second prints the contents of the tag object. The output has this shape, with your own hashes, name, and timestamp:
object 3f1c0e5a7d1b2c4e8f9a0b1c2d3e4f5a6b7c8d9e
type commit
tag v1
tagger Ada Lovelace <[email protected]> 1764862596 +0000
first releaseThe tag object is plain text with a small set of headers:
objectis the hash of the tagged object.typeis that object's type. It is stored so Git can know the type without reading the target.tagis the tag's name.taggeris the identity, a Unix timestamp, and a time zone offset.- After a blank line comes the message.
git rev-parse v1 prints the hash of the tag object. git rev-parse 'v1^{commit}' prints the hash of the commit. These two hashes differ, which shows that the name v1 resolves to an object that is not the commit itself. The quotes around v1^{commit} stop the shell from treating the braces as special.
Peeling
Following a tag to the object it points to is called peeling. The suffix ^{type} peels a name until it reaches an object of that type.
git rev-parse 'v1^{}'
git rev-parse 'v1^{commit}'
git rev-parse 'v1^{tree}'
git cat-file -p 'v1^{tree}'^{} with nothing inside peels until the result is not a tag. ^{tree} peels through the tag and the commit, and returns the root tree. Most Git commands that expect a commit peel tags for you. That is why git checkout v1, git log v1, and git diff v1 v2 work with either kind of tag.
For lightweight tags, peeling does nothing, because the ref already points at the commit.
How the ref and the object fit together
Look at the arrow from refs/heads/main. A branch points at the commit directly. The tag has an extra step, which is where the tagger, date, and message live.
Inspecting tag refs
You can see the difference between kinds of tag without opening each object. The %(objecttype) field shows what each ref points at:
git tag v0-light
git for-each-ref refs/tags --format='%(refname:short) %(objecttype) %(objectname:short)'
cat .git/refs/tags/v1
cat .git/refs/tags/v0-lightv1 is listed as type tag. v0-light is listed as type commit. The two files under .git/refs/tags/ each contain one hash, and only the lightweight tag's hash matches git rev-parse HEAD.
git show-ref has a -d option that dereferences tags. It prints an extra line for each annotated tag, ending in ^{}:
git show-ref -d --tagsThe line for refs/tags/v1 shows the tag object hash. The line for refs/tags/v1^{} shows the commit hash it peels to.
Because a tag file may be missing from .git/refs/tags/ after git pack-refs (which git gc runs), do not rely on reading files there. Use git for-each-ref or git show-ref. Packed refs live in .git/packed-refs, and that file also records peeled values for annotated tags on lines starting with ^.
Building a tag by hand
You can also make the tag object yourself, as you did for commits. git mktag reads a tag object's text from standard input, checks that it is well formed, writes it to the object database, and prints its hash. It does not create a ref.
commit=$(git rev-parse HEAD)
tag=$(printf 'object %s\ntype commit\ntag v2\ntagger Ada <[email protected]> 1700000000 +0000\n\nsecond release\n' "$commit" | git mktag)
echo "$tag"
git update-ref refs/tags/v2 "$tag"
git cat-file -t v2
git cat-file -p v2This follows the same pattern as the manual commit: write the object first, then make a ref point at it. git mktag is strict. If the type line does not match the real type of the target, or a header is missing, it refuses to write the object. This makes it useful for learning the format.
The timestamp here is fixed at 1700000000. git tag -a fills in the current time and your configured identity.
Tagging things other than commits
A tag object can point at any object type. The type header records which one. Annotated tags on commits are by far the most common. The Git project itself has a tag pointing at a blob, which holds a maintainer's public key. You can try it:
blob=$(git rev-parse HEAD:second.txt)
git tag -a second-file-note -m 'a note about one file' "$blob"
git cat-file -p second-file-noteThe type line in the output says blob. Peeling with ^{commit} on this tag fails, because a blob has no commit to peel to. Delete the experiment with git tag -d second-file-note.
Signed tags
A signed tag is an annotated tag whose message ends with a signature block. git tag -s v3 -m '...' signs with your GPG key (or an SSH or X.509 key, depending on gpg.format). The signature covers the headers and the message, and so covers the hash of the target. The target hash in turn covers the whole history behind it. git tag -v v3 verifies the signature.
You need a configured signing key to run these commands, so they are not part of the demo. The point to remember is that only annotated tags can be signed, because only they have a body to hold the signature.
Behavior that depends on the tag kind
Several everyday commands treat the two kinds differently:
git describeconsiders only annotated tags unless you pass--tags.git push --follow-tagspushes annotated tags that are reachable from the pushed commits. It skips lightweight tags.git show v1prints the tag object's details (tagger, date, message) and then the commit. For a lightweight tag it prints only the commit.git tag -l -nprints the first line of an annotated tag's message. For a lightweight tag it prints the commit's subject line.
Moving and deleting tags
A tag is not meant to move, but you can force it. git tag -f -a v1 -m 'redo' <commit> writes a new tag object and rewrites refs/tags/v1 to point at it. Git does not delete the old tag object. Nothing refers to it any more, so it becomes unreachable. git tag -d v1 removes only the ref, with the same result.
Check this yourself, using the commands from the reachability chapter:
old=$(git rev-parse v1)
git tag -d v1
git cat-file -t "$old"
git fsck --unreachableThe cat-file command still prints tag, and fsck lists the old tag object as unreachable. It stays in the database until git gc prunes it. By default, pruning only removes unreachable objects older than the expiry time of two weeks, so it will not disappear right away.
Re-create the tag with git tag -a v1 -m 'first release' before moving on. The new object has a new timestamp, so its hash differs from the first one.
Why the extra object is worth having
A tag object gives you a stable record of who marked a point in history, when, and why. The tag has its own hash, so a signature can cover it. The ref refs/tags/v1 is still just a name, and the name can be moved. The tag object does not change. If you need to know that a release really pointed at a given commit, you check the object, not the name.
Exercise
In a repository with one commit, you run git tag light and git tag -a note -m 'msg'. Which of the two tags adds an object to .git/objects, and what would git cat-file -t print for each name?
Show answer
Only note adds an object, a tag object. git cat-file -t light prints commit, because refs/tags/light holds the commit hash directly. git cat-file -t note prints tag, because the ref holds the hash of the tag object, which in turn names the commit.
Exercise
What does the suffix in git rev-parse 'note^{commit}' do, and why do you need it to compare the tag with HEAD?
Show answer
^{commit} peels the name: Git follows the tag object to the commit it points at and prints that commit's hash. Plain git rev-parse note prints the tag object's hash, which never equals the hash of HEAD. With peeling, git rev-parse 'note^{commit}' equals git rev-parse HEAD when the tag is on the current commit.
Exercise
You delete an annotated tag with git tag -d v1. What happens to the tag object, and how can you see it?
Show answer
Only the ref refs/tags/v1 is removed. The tag object stays in the object database as an unreachable object, until git gc prunes it after the expiry period. git fsck --unreachable lists it, and git cat-file -p <hash> still prints its contents if you saved the hash.