More Git answers

by Danny
Published 3 min read

The simplest way to sign Git commits is with the SSH key you already use. Set gpg.format to ssh, point user.signingkey at your public key, turn on commit.gpgsign and add the key to your host.

The short answer

#

Use your existing SSH key. Tell Git to sign with SSH, point it at your public key, and sign every commit by default. Then add the same key to GitHub or GitLab as a signing key so commits show as verified.


_10
git config --global gpg.format ssh
_10
git config --global user.signingkey ~/.ssh/id_ed25519.pub
_10
git config --global commit.gpgsign true
_10
git config --global tag.gpgsign true

Why it happens

#

Git lets anyone write any name and email into a commit. user.name and user.email are just settings, so a commit that says it is from you proves nothing. Signing adds a cryptographic signature that only the holder of your private key can produce. Hosts and other developers can check it against your public key.

For years signing meant setting up GPG, which many people found fiddly. Since Git 2.34, Git can sign with SSH keys too. Most developers already have an SSH key for pushing, so this is now the shortest route. The settings still have "gpg" in their names for historical reasons, even when the format is SSH.

How to fix it

#

Sign with an SSH key

Check your Git version, then find or create a key.


_10
git --version
_10
ls ~/.ssh/*.pub
_10
# if you have none:
_10
ssh-keygen -t ed25519 -C "[email protected]"

Configure Git to use it.


_10
git config --global gpg.format ssh
_10
git config --global user.signingkey ~/.ssh/id_ed25519.pub
_10
git config --global commit.gpgsign true
_10
git config --global tag.gpgsign true

If your key has a passphrase, load it into ssh-agent so you are not asked for it on every commit. You can also sign one commit at a time with git commit -S instead of turning on commit.gpgsign.

You can reuse your authentication key or make a separate one just for signing. A separate key lets you revoke one without affecting the other.

Add the key to your Git host

The host can only verify the signature if it knows your public key.

  • GitHub: Settings, SSH and GPG keys, New SSH key, and choose Signing Key as the key type. A key added only as an authentication key does not verify commits, so you may need to add the same key twice, once of each type.
  • GitLab: Preferences, SSH Keys, and choose a usage type that includes signing.

The email in your commits must match a verified email on your account, or the host shows the signature as unverified.

Verify signatures locally

Git needs a list of trusted keys before it can check SSH signatures on your machine. Create an allowed signers file and tell Git where it is.


_10
echo "[email protected] $(cat ~/.ssh/id_ed25519.pub)" >> ~/.config/git/allowed_signers
_10
git config --global gpg.ssh.allowedSignersFile ~/.config/git/allowed_signers

Then check a commit.


_10
git log --show-signature -1
_10
git verify-commit HEAD

You should see a line starting "Good "git" signature for [email protected]". Without the allowed signers file, Git reports that it cannot verify the signature, even if the host shows it as verified.

Sign with GPG instead

If your team already uses GPG keys, the setup is the same apart from the format and key.


_10
gpg --list-secret-keys --keyid-format=long
_10
git config --global gpg.format openpgp
_10
git config --global user.signingkey 3AA5C34371567BD2
_10
git config --global commit.gpgsign true

openpgp is the default format, so you only need to set it if you switched to SSH earlier.

Common pitfalls

#
  • Email mismatch. The commit email must match a verified address on your account, or the commit shows as unverified.
  • Adding the key only for authentication. On GitHub, add it as a signing key as well.
  • Old Git versions. SSH signing needs Git 2.34 or later. Older versions ignore or reject gpg.format ssh.
  • "No principal matched" when verifying locally. The email in your allowed signers file does not match the commit's email.
  • Signing old commits. Signing applies to new commits. Re-signing existing ones means rewriting history, for example with git rebase --exec "git commit --amend --no-edit -S", which changes their hashes.
  • GUI tools and IDEs. Some use their own Git binary or settings. Check they pick up your global config.
#

Was this resource helpful?