Sub topic 7
Signing Tags
This chapter assumes you can already commit, tag, and read git log, and that you have a signing key configured as covered in the previous chapters (either a GPG key or an SSH key, with user.signingkey set). It covers how signed tags work, how to create and verify them, and the errors you are most likely to meet.
Why tags get their own signature
A commit signature covers one commit. A release tag points at a commit and names it, for example v1.0.0. Consumers of your project trust that name. They download the tag, build it, and ship it. A signed tag lets them check that the person who holds your key created that tag, and that the tag still points at the commit it pointed at when you signed it.
A signature on the commit alone does not give you this. Someone could point a new tag at a validly signed commit, and nothing would show who made the tag. Signing the tag object closes that gap.
A signed tag proves that a holder of the key created the tag object. It does not prove the code is safe, and it says nothing about who wrote each commit beneath it.
Lightweight and annotated tags
Git has two kinds of tag:
| Kind | What it is | Can be signed? |
|---|---|---|
| Lightweight | A name that points straight at a commit. No object of its own. | No |
| Annotated | A tag object with a tagger, date, message, and target. | Yes |
Only annotated tags can carry a signature, because the signature is stored inside the tag object. Git makes a tag annotated whenever you pass -a, -s, or -m. A plain git tag v1.0.0 creates a lightweight tag, and no setting will sign it.
Creating a signed tag
Use -s (short for --sign) to create a signed, annotated tag:
git tag -s v1.0.0 -m "Release 1.0.0"Git builds the tag object, sends its text to the signing program, and appends the signature to the tag message. The program is GnuPG by default. It is ssh-keygen if gpg.format is ssh. You do not pick the backend in the command. Git reads it from configuration.
To tag a commit other than the current one, add its hash or a ref:
git tag -s v0.9.0 -m "Release 0.9.0" 4f2a9c1If you omit -m, Git opens your editor so you can write the message. A signed tag needs a message. Without one, Git fails instead of creating an unsigned tag.
Signing every tag automatically
Commit signing has commit.gpgsign. Tags have their own setting, tag.gpgSign:
git config --global tag.gpgSign trueWith this set, every annotated tag you create is signed. This setting is separate from commit.gpgsign, so enabling one does not enable the other. In a repository where you have enabled only commit.gpgsign, git tag -a v1.0.0 -m "..." still produces an unsigned tag. Setting tag.gpgSign is the safer choice for anyone who cuts releases.
The -u option signs with a specific key for one tag. It overrides user.signingkey:
git tag -u 3AA5C34371567BD2 v1.0.0 -m "Release 1.0.0"With SSH signing, the argument to -u is a public key file or a key literal, not a GPG key ID.
A full setup for SSH-signed tags
This sequence creates an Ed25519 key and configures Git (2.34 or later) to sign both commits and tags with it:
ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/id_ed25519_signing
git config --global gpg.format ssh
git config --global user.signingkey ~/.ssh/id_ed25519_signing.pub
git config --global commit.gpgsign true
git config --global tag.gpgSign trueNotice that user.signingkey points to the .pub file. Git hands the public key to ssh-keygen -Y sign, which finds the matching private key through your SSH agent or the file next to it.
What a signed tag contains
A signed tag is a single object. You can print it with git cat-file:
git cat-file -p v1.0.0The output has the header lines object, type, tag, and tagger. After them come your message and the signature block. For GPG, that block starts with -----BEGIN PGP SIGNATURE-----. For SSH, it starts with -----BEGIN SSH SIGNATURE-----.
The signature covers the header and the message. The header names the target commit by hash, so the signature protects the tag's link to that commit. Changing the target or the message breaks the signature.
Verifying tags
Verify a tag with git tag -v:
git tag -v v1.0.0Git prints the tag contents followed by the result from the signing tool. It exits with status 0 for a good signature and a non-zero status otherwise, so scripts can rely on the exit code.
You can also see signature details through git show:
git show --show-signature v1.0.0Both commands run the same check. git tag -v is the more direct choice in release scripts.
Verification for GPG tags
For GPG, the verifier needs the tagger's public key in your keyring. If you do not have it, you see a failure like Can't check signature: No public key. Import the key first, then verify again. A good result shows Good signature from "Alice <[email protected]>". You may also see WARNING: This key is not certified with a trusted signature!. That warning is about your local trust settings, not the signature. The signature is mathematically valid, but you have not told GnuPG that you trust the key's owner. The earlier chapter on GPG verification covers trust levels.
Verification for SSH tags
For SSH, Git needs an allowed signers file that maps identities to public keys. This is the same file used for commits:
mkdir -p ~/.config/git
echo "[email protected] $(cat ~/.ssh/id_ed25519_signing.pub)" >> ~/.config/git/allowed_signers
git config --global gpg.ssh.allowedSignersFile ~/.config/git/allowed_signers
git tag -v v1.0.0Without the file, Git cannot check the signature. The error is gpg.ssh.allowedSignersFile needs to be configured and exist for ssh signature verification. When the file exists but does not list the signer, the check reports that no principal matched.
The identity Git looks up for a tag is the tagger's email address from the tag header. If the email in the tag differs from the principal in the file, verification fails even though the key is right.
Verifying a tag before you build
Signing helps only if someone checks. A release process should verify before it builds:
git fetch --tags origin
git tag -v v1.0.0 && git checkout v1.0.0The && stops the checkout if verification fails. Note that git fetch --tags brings the tag objects, including their signatures, so verification works offline after the fetch.
Pushing and replacing tags
git push origin v1.0.0 sends a single tag. git push --tags sends all tags, including ones you do not mean to publish. Push the tag you intend.
Git does not overwrite an existing remote tag unless you force it. Resigning a tag under the same name changes which object the name points to. Anyone who already fetched the old tag keeps it, because git fetch does not move existing tags by default. Treat a published tag as permanent. If a release is wrong, create a new version, such as v1.0.1.
Common problems
| Symptom | Likely cause | Fix |
|---|---|---|
| `fatal: no tag message?` | `-s` used with no message and an empty editor buffer | Pass `-m` or write a message in the editor |
| `gpg failed to sign the data` | Passphrase prompt cannot appear | Set `GPG_TTY` and test GnuPG directly |
| `Can't check signature: No public key` | Tagger's key is not in your keyring | Import the public key |
| `No principal matched` | Signer or email is missing from allowed signers | Add the identity and key to the file |
| New tag is unsigned | Only `commit.gpgsign` is set | Set `tag.gpgSign` or use `-s` |
The passphrase failure is the most common. Test GnuPG outside Git to separate the two layers:
export GPG_TTY=$(tty)
echo "test" | gpg --clearsignIf this works, add the export line to your shell profile and retry the tag. If it fails, the problem is in GnuPG or the key, not in Git.
Exercises
Exercise
You have set commit.gpgsign true and nothing else. You run git tag -a v2.0.0 -m "Release 2.0.0". Is the tag signed, and what should you change so future annotated tags are signed without extra flags?
Show answer
The tag is not signed. commit.gpgsign affects only commits. Run git config --global tag.gpgSign true, or pass -s for each tag.
Exercise
A teammate runs git tag -v v1.0.0 in a repository where gpg.format is ssh. Git reports that gpg.ssh.allowedSignersFile must be configured. What do they need to create and configure, and what must each line in the file contain?
Show answer
They need an allowed signers file and a gpg.ssh.allowedSignersFile setting that points to it. Each line holds a principal, usually the tagger's email, followed by the public key, for example [email protected] ssh-ed25519 AAAA....
Exercise
Why can a lightweight tag not be signed, and which command creates a signed tag with the message "Release 1.2.0"?
Show answer
A signature is stored inside a tag object, and a lightweight tag has no object of its own because it is only a name pointing at a commit. Use git tag -s v1.2.0 -m "Release 1.2.0".