Sub topic 7
Signing tags
This chapter assumes you can already sign commits, as covered in the previous chapter. You should have a signing key configured with user.signingkey and, for SSH, gpg.format set to ssh. It also assumes you know the difference between lightweight and annotated tags.
Why tags need their own signature
A signed commit vouches for one snapshot and its history. A release tag says something different: "this commit is version 1.0.0, and I stand behind it." Package builders and downstream users often check the tag, not every commit before it. Signing the tag lets them confirm that the release label came from a known key.
Only annotated tags can be signed. An annotated tag is a full Git object with a tagger, a date, and a message. A lightweight tag is just a name pointing at a commit, so it has nowhere to store a signature.
Creating a signed tag
Use -s (short for --sign) with a message:
git tag -s v1.0.0 -m "Release 1.0.0"Git uses the key from user.signingkey and the format from gpg.format. The signature is appended to the tag message inside the tag object.
If you want every tag signed without typing -s, set this once:
git config --global tag.gpgSign trueWith that setting, git tag -a v1.0.0 -m "..." signs the tag too. Be aware that it also applies to any annotated tag you create, including tags made by scripts.
To sign a tag that already exists, create it again with -f. This replaces the tag, so use it only for tags you have not pushed.
git tag -s -f v1.0.0 -m "Release 1.0.0" <commit>Verifying a tag
Check a tag with git tag -v:
git tag -v v1.0.0Git prints the tag contents and the output of the signature check. The command exits with a non-zero status if the signature is bad or missing, so scripts can use it as a gate. You can also use git verify-tag v1.0.0, which does the same job.
For SSH-signed tags, verification needs the allowed signers file from the SSH verification chapter. Without gpg.ssh.allowedSignersFile, Git cannot tell you whether the key is trusted and reports an error.
flowchart TDDo not rely on the line above. The real diagram follows.
Pushing and fetching
Tags are not pushed by default. Push the one you made:
git push origin v1.0.0Pushing a tag sends the signature with it. Anyone who fetches the tag can verify it locally, provided they have your public key (GPG) or your entry in their allowed signers file (SSH).
What a tag signature covers
The signature covers the tag object: the target commit's hash, the tag name, the tagger, and the message. Because the commit hash depends on all earlier history, a valid signature pins the whole history up to that commit. It does not prove that the code is safe, only that the key holder created this tag.
Common errors
- "cannot sign a lightweight tag": you used
-swithout a message in a way that created no annotated object, or the tag already exists as lightweight. Delete it and recreate it with-s -m. - "failed to sign the data" or a missing prompt: for GPG, set
export GPG_TTY=$(tty)so the passphrase prompt can appear. - "No signature" from
git tag -v: the tag is annotated but unsigned, or lightweight. Recreate it with-s. - Verification fails for SSH tags: the tagger email in the tag must match a principal in the allowed signers file.
Exercise
You run git tag v2.0.0 and then git tag -v v2.0.0. Verification fails. Why, and how do you fix it?
Show answer
git tag v2.0.0 creates a lightweight tag, which cannot hold a signature. Delete it with git tag -d v2.0.0 and recreate it as a signed annotated tag with git tag -s v2.0.0 -m "Release 2.0.0". Then git tag -v v2.0.0 should succeed.