Sub topic 6
Signing commits
This chapter assumes you have already created a signing key (GPG or SSH) and set user.signingkey and, for SSH, gpg.format. It also assumes you know basic git commit and git log.
What signing a commit does
When you sign a commit, Git hashes the commit object, which includes the tree, parent, author, committer, and message. It then asks your signing program to sign that data. The signature is stored in a gpgsig header inside the commit object. Changing any part of the commit changes its hash and invalidates the signature.
Signing one commit
Add -S to sign a single commit:
git commit -S -m "Add release notes"If you use a specific key, -S<keyid> overrides user.signingkey for that commit. Note that there is no space between -S and the key ID.
Signing every commit
Setting commit.gpgsign makes signing the default. Despite the name, it applies to SSH signing too:
git config --global commit.gpgsign trueTo skip signing for one commit, for example in a throwaway repository, use:
git commit --no-gpg-sign -m "WIP"You can also set the option per repository by running the command without --global inside that repository.
Checking the result
Show the signature on the latest commit:
git log --show-signature -1
git verify-commit HEADFor GPG, a good result prints Good signature from .... For SSH, Git can only say Good "git" signature with your key fingerprint until you configure an allowed signers file. A later chapter covers that file. A one-line view of every commit's status is available with a format placeholder:
git log --pretty="format:%h %G? %an %s"The %G? placeholder prints one letter per commit:
| Letter | Meaning |
|---|---|
| `G` | Good signature |
| `B` | Bad signature |
| `U` | Good signature, unknown validity (key not trusted) |
| `E` | Signature cannot be checked, such as a missing key |
| `N` | No signature |
Signing existing commits
Git signs a commit only when it creates one. To sign the most recent commit that you have not pushed, amend it:
git commit --amend --no-edit -STo sign a range, rebase with the --exec option:
git rebase --exec 'git commit --amend --no-edit -n -S' -i origin/mainBoth commands create new commits with new hashes. Do not use them on commits others have already pulled.
Common errors
gpg failed to sign the data. GnuPG cannot show its passphrase prompt. SetGPG_TTYand test GnuPG on its own:
``bash export GPG_TTY=$(tty) echo "test" | gpg --clearsign ``
If this works, Git will work too. Add the export line to your shell profile.
- Commit is signed but shows as unverified on the host. The email in your commit (
user.email) must match an email on the key (GPG) or on your hosting account. Check withgit config user.email.
error: Load key ... invalid formator a missing-file error with SSH.user.signingkeyshould point to the public key file (.pub) whengpg.formatisssh, and the matching private key must be in your agent or at the same path without.pub.
- Signing works in the terminal but not in an editor or GUI. The tool may not have your agent socket or
GPG_TTY. Start it from a shell where signing works to confirm.
Exercise
You enabled commit.gpgsign true and now git commit fails with "failed to sign the data". What single command tests whether GnuPG itself is the problem, and what do you change if it fails?
Show answer
Run echo "test" | gpg --clearsign. If it fails to prompt for a passphrase, set export GPG_TTY=$(tty) (and add it to your shell profile) so pinentry can use your terminal, then retry the commit.
Exercise
You committed three local commits without signing. How can you sign the most recent one only, and why does its hash change?
Show answer
Run git commit --amend --no-edit -S. The signature is stored inside the commit object, so adding it changes the object's contents and therefore its hash.