Sub topic 6
Signing commits
This chapter covers how Git attaches a signature when it creates a commit, how to sign by default or on demand, and how to check the result. It assumes you have already created a key and set user.signingkey and gpg.format as described in the earlier chapters. It also assumes you know git commit, git log, and git config.
What gets signed
A Git commit is an object made of a few headers (tree, parent, author, committer) followed by the message. When you sign, Git hands that text to a signing program. The program returns a signature, and Git stores it in an extra gpgsig header. The header is called gpgsig even when the signature is an SSH one.
The commit ID is a hash of the whole object, including the signature header. Because the hash covers the tree and the parent IDs, a valid signature also covers the full file contents and the history behind the commit. Changing any of it gives a different commit ID, and the old signature no longer matches.
The signature proves that someone holding the private key approved this exact commit content. It does not prove that the author name is true, and it does not prove the person is trustworthy. Whether the key belongs to a particular person is a separate question. Trust settings, an allowed signers file, or a hosting platform answer it.
Signing one commit at a time
Use -S (long form --gpg-sign) to sign a single commit:
git commit -S -m "Add release notes"With no argument, Git signs with the key named in user.signingkey. If that setting is empty, Git falls back to a key that matches your committer identity. That works for GPG. For SSH it does not, so always set user.signingkey for SSH signing.
You can also name a key for one commit. There must be no space between -S and the key ID:
git commit -S3AA5C34371567BD2 -m "Sign with a specific GPG key"For SSH signing, user.signingkey can be a path to a public key file. It can also be a literal public key with a key:: prefix, which is useful when the key lives only in an agent:
git config --global user.signingkey "key::ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI..."Signing every commit
Set commit.gpgsign so you do not have to remember -S:
git config --global commit.gpgsign trueThis setting applies to every command that creates a commit, not only git commit. Merges, cherry-picks, reverts, and rebases will also try to sign. If the signing program is unavailable, those commands fail instead of producing an unsigned commit.
To skip signing once, for a throwaway commit:
git commit --no-gpg-sign -m "WIP: scratch work"You can also set the option per repository. Run git config commit.gpgsign true without --global inside a repository, and use a different key there. This is the usual way to keep a work key and a personal key apart.
| Command | Effect |
|---|---|
| `git commit -S` | Sign this commit only |
| `git commit --no-gpg-sign` | Do not sign this commit, even if `commit.gpgsign` is true |
| `git config commit.gpgsign true` | Sign every commit in this repository |
| `git config --global commit.gpgsign true` | Sign every commit for your user |
| `git merge -S`, `git cherry-pick -S`, `git revert -S` | Sign the commit these commands create |
A worked example
This example builds a throwaway repository and signs one commit with an SSH key. It assumes the ~/.ssh/id_ed25519_signing key from the earlier chapter exists and Git is 2.34 or later.
git init signing-demo
cd signing-demo
git config user.name "Alice"
git config user.email "[email protected]"
git config gpg.format ssh
git config user.signingkey ~/.ssh/id_ed25519_signing.pub
echo "one" > a.txt
git add a.txt
git commit -S -m "Add a.txt"
git cat-file -p HEADThe last command prints the raw commit. After the committer line you will see a gpgsig header that begins with -----BEGIN SSH SIGNATURE-----. For a GPG key it begins with -----BEGIN PGP SIGNATURE-----.
Because git config ran without --global, these settings apply only to signing-demo.
Checking the result
git log --show-signature prints the verifier's output under each commit:
git log --show-signature -1For a script-friendly view, use the signature placeholders in --format:
git log --format="%h %G? %GS %GK %s" -5%G? is a one-letter status, %GS is the signer, and %GK is the key ID or fingerprint.
| `%G?` | Meaning |
|---|---|
| `G` | Good signature |
| `B` | Bad signature |
| `U` | Good signature, validity unknown or untrusted |
| `X` | Good signature that has expired |
| `Y` | Good signature made by an expired key |
| `R` | Good signature made by a revoked key |
| `E` | Signature cannot be checked, for example the public key is missing |
| `N` | No signature |
A U for a GPG commit is common. The signature is mathematically fine, but you have not told GPG to trust the key. For SSH, an E-style failure usually means gpg.ssh.allowedSignersFile is not set or does not list the signer.
You can verify a single commit with git verify-commit HEAD. It exits with a non-zero status when verification fails, so it works in scripts.
Signer and committer are separate things
Git does not check that the signing key belongs to the name and email in the commit. You can set user.email to anything, sign with your real key, and Git will still store both.
This matters for hosting platforms. They typically match the committer email against an email address verified on your account, and also match the signing key against keys you registered. If either fails, the commit shows as unverified even though git log --show-signature reports a good signature locally. Check the committer line:
git log -1 --format="committer: %cn <%ce>"Signing commits you already made
You can sign the most recent commit by amending it:
git commit --amend --no-edit -STo sign several commits, rebase and have Git sign the ones it rewrites:
git rebase --exec "git commit --amend --no-edit -S" origin/mainYou can also use git rebase -S origin/main, which signs every commit the rebase replays.
Both approaches create new commits with new IDs. If you already pushed the old commits, you will need to force-push, and anyone who based work on them must rebase. A later chapter covers rebasing and squashing in more detail.
Common failures
error: gpg failed to sign the data. The signing program could not run or could not ask for a passphrase. For GPG, runexport GPG_TTY=$(tty)and retry. Test GnuPG outside Git withecho test | gpg --clearsign.error: Load key ... invalid formatorNo private key found. For SSH signing,user.signingkeymust point at the public key, and the matching private key must be loadable, either from disk or from your agent.gpg.ssh.allowedSignersFile needs to be configured. Git can create SSH signatures without this setting but cannot verify them. See the allowed signers chapter.- Status
Eon GPG commits from teammates. You do not have their public key. Import it withgpg --import. - Signing silently skipped. A repository-level
commit.gpgsign falseoverrides the global setting. Rungit config --show-origin --get-all commit.gpgsignto see every value and where it comes from.
Exercise
You run git log --format="%h %G? %s" -3 and see a1b2c3d G, e4f5a6b U, and c7d8e9f N for three commits. What does each letter tell you, and which commit would you investigate first if the repository uses GPG and you expect every commit to be signed?
Show answer
G is a good, trusted signature. U is a good signature whose key is not trusted or whose validity is unknown, which is common until you set trust on the signer's key. N means no signature at all. Investigate c7d8e9f first. It has no signature, so either the author did not sign it or commit.gpgsign was off or overridden when it was created.
Exercise
Your signed commits show a good signature in git log --show-signature, but your hosting platform marks them unverified. Name two things to check.
Show answer
Check that the committer email (git log -1 --format="%ce") matches an email verified on your platform account. Also check that the public key you used for signing is registered on the platform as a signing key. For SSH, an authentication-only key registration does not count as a signing key.