Sub topic 2
How Git signs: OpenPGP, SSH, and X.509 backends
This chapter assumes you can commit, tag, and read git log, and that you know what a public and a private key are. It explains how Git produces and stores a signature, which three backends it can use, and how gpg.format selects one. Creating keys comes in the next chapters.
Git does not do cryptography itself
Git contains no signing code. When you ask it to sign, it runs an external program, hands that program the data to sign, and stores whatever signature comes back. Git calls this program the backend. Three signature formats are supported:
| `gpg.format` value | Signature type | Default program | Added in |
|---|---|---|---|
| `openpgp` (default) | OpenPGP detached signature | `gpg` | Original |
| `x509` | S/MIME (CMS) signature | `gpgsm` | Git 2.19 |
| `ssh` | SSH signature | `ssh-keygen` | Git 2.34 |
The setting is called gpg.format for historical reasons, even when no GnuPG is involved. If you never set it, Git uses openpgp.
What happens during a signed commit
When you run git commit -S, Git first builds the commit object without a signature. That object holds the tree hash, the parent hashes, the author and committer lines, and the message. Git sends this text to the backend, and the backend returns a detached signature. Git then inserts the signature into the commit as a gpgsig header and computes the commit hash over the complete object, signature included.
Three consequences follow from this design.
- The signature covers the tree, parents, author, committer, and message. Changing any of them invalidates it.
- The signature is part of the object, so the commit hash includes it. Amending or rebasing a signed commit creates a new object that needs a new signature.
- Git does not sign the files directly. It signs the commit, and the tree hash inside the commit pins the files.
You can see the stored signature with git cat-file. The header continues over several lines, and each continuation line starts with one space.
git cat-file -p HEAD
# tree 9f1c...
# parent 2b7e...
# author Alice <[email protected]> 1767225600 +0000
# committer Alice <[email protected]> 1767225600 +0000
# gpgsig -----BEGIN SSH SIGNATURE-----
# U1NIU0lH...
# -----END SSH SIGNATURE-----
#
# Add release notes
# Print only the signature block, whichever format was used:
git cat-file commit HEAD | sed -n '/^gpgsig/,/^ -----END/p'For an OpenPGP signature the block begins with -----BEGIN PGP SIGNATURE-----. For X.509 it begins with -----BEGIN SIGNED MESSAGE-----.
Where tag signatures live
Tags work differently. A signed tag must be an annotated tag object, and Git appends the signature to the end of the tag message. There is no header. Lightweight tags are only references, so they cannot carry a signature. git tag -s always creates an annotated tag. The tags chapter covers this in detail.
The three backends
OpenPGP (GnuPG)
This is the oldest and most widely supported option. Git runs roughly gpg --status-fd=2 -bsau <key>, which makes a detached, ASCII-armored signature with the chosen key. The --status-fd=2 option makes GnuPG write machine-readable status lines to standard error. Git reads those lines when it verifies.
GnuPG has a built-in web of trust. It can record how much you trust each key's owner, and it supports subkeys, expiry dates, and revocation certificates. The cost is heavier tooling: a keyring, an agent, and a pinentry program for passphrases.
SSH
SSH signing reuses a key pair you probably already have. Git runs ssh-keygen -Y sign -n git -f <key>. The -Y sign option creates an SSH signature, and -n git sets the namespace to git. The namespace is part of what gets signed. A signature made for Git cannot be replayed as a valid signature for another SSH use, such as signing files for a different tool.
Two points matter for SSH:
- The key in
user.signingkeyis a public key file path, or a literal public key. If the private half is protected byssh-agent, ssh-keygen finds it there. - SSH keys have no web of trust and no built-in notion of "whose key is this". Verification relies on a file you maintain, the allowed signers file, which maps identities to public keys. The verification chapter shows how to set one up.
You need Git 2.34 or later. You also need an OpenSSH recent enough to support -Y sign and -Y verify, which in practice means OpenSSH 8.1 or later.
X.509 (S/MIME)
With gpg.format x509, Git calls gpgsm, which signs with an X.509 certificate issued by a certificate authority. Organizations that already run an internal public key infrastructure sometimes choose this because certificates tie into existing identity and revocation systems. It is the least common choice for open source work. Hosting platforms vary in what they accept. Check yours before adopting it.
How `user.signingkey` is interpreted
The same setting name means different things depending on the format.
- With
openpgporx509, it is a key ID, fingerprint, or email that GnuPG can resolve. If it is unset, Git passes your committer identity (Name <email>) to GnuPG, which looks for a matching key. - With
ssh, it is the path to a public key file, or the key text itself. A literal key must be written either with akey::prefix, as inkey::ssh-ed25519 AAAA..., or as a string starting withssh-. If it is unset, Git runs the command ingpg.ssh.defaultKeyCommand, if you configured one. Otherwise signing fails.
Switching formats therefore means changing both gpg.format and user.signingkey. Changing only the format leaves a GPG key ID where Git expects an SSH key path, and the signing step fails.
Choosing the program
Git looks for a program in this order, from most to least specific:
gpg.<format>.program, for examplegpg.ssh.program.gpg.program, which applies to OpenPGP only. It is the legacy setting. Use the format-specific setting for new configurations.- The built-in default from the table above.
This is useful when the right binary is not first on your PATH. A common case is macOS, where the system ssh-keygen may lack features that a Homebrew OpenSSH provides, and Windows setups that need an explicit path.
# Use SSH signing with an explicit ssh-keygen
git config --global gpg.format ssh
git config --global gpg.ssh.program /usr/bin/ssh-keygen
# Use OpenPGP with a specific GnuPG binary
git config --global gpg.format openpgp
git config --global gpg.openpgp.program /usr/local/bin/gpg
# Inspect what Git will actually use
git config --get gpg.format
git config --get-regexp '^(gpg|user\.signingkey)'Mixing formats across repositories
Because the format is ordinary Git configuration, it can differ per repository. Set it with git config --local inside a repository to override the global value. Another approach is a conditional include, which keeps your work and personal identities apart without touching each repository.
# ~/.gitconfig
[gpg]
format = ssh
[user]
signingkey = ~/.ssh/id_ed25519_personal.pub
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-work# ~/.gitconfig-work
[gpg]
format = openpgp
[user]
signingkey = 3AA5C34371567BD2Every repository under ~/work/ then signs with OpenPGP, and everything else signs with SSH. Both settings change together in the included file, which avoids the mismatch described above.
Choosing a backend
| Situation | Reasonable choice | Why |
|---|---|---|
| You already authenticate to your host with SSH keys and want little setup | SSH | No keyring or agent beyond `ssh-agent`. Fewer moving parts. |
| You need expiry dates, subkeys, or revocation certificates tied to the key | OpenPGP | These are native OpenPGP features. |
| Your organization issues certificates from its own CA | X.509 | Signing ties into existing certificate management. |
| You verify third-party signatures with a web of trust | OpenPGP | SSH has no trust model beyond your allowed signers file. |
| You use a hardware token such as a FIDO2 security key | SSH or OpenPGP | Both can use hardware-backed keys. Setup differs. |
A signed commit proves that someone holding the private key approved this exact content. It does not prove that the person is who the author line claims, and Git itself never checks that. Whether a signature means anything depends on whether the verifier can map a public key to a person, which is the job of the trust model or the allowed signers file.
Common mistakes
- Setting
gpg.format sshwhileuser.signingkeystill holds a GPG key ID. - Pointing
user.signingkeyat the private key file for SSH signing. Use the.pubfile. - Running Git older than 2.34 with SSH signing. Git then ignores the format or reports an error about an unsupported format.
- Assuming an amended or rebased commit keeps its old signature. The new object needs a new signature, and Git signs it only if signing is on at that moment.
Exercise
You switch your global config to gpg.format ssh, but git commit now fails to sign. user.signingkey is still 3AA5C34371567BD2. What is wrong, and what two things must you provide for SSH signing to work?
Show answer
Under the ssh format, Git treats user.signingkey as a path to a public key file or a literal public key, not as a GPG key ID, so ssh-keygen cannot use 3AA5C34371567BD2. Set user.signingkey to your SSH public key (for example ~/.ssh/id_ed25519_signing.pub) and make sure the matching private key is available, either on disk or loaded in ssh-agent. Also confirm that Git is 2.34 or later and that OpenSSH supports -Y sign.
Exercise
Where does Git store the signature of a signed commit, and where does it store the signature of a signed tag? Why does amending a signed commit produce a different hash even if you change nothing?
Show answer
A commit stores the signature in a gpgsig header inside the commit object. A tag stores it at the end of the annotated tag message. The signature is part of the object, and the commit hash is computed over the whole object. An amend creates a new commit with a new committer timestamp, so the signed text differs, the signature differs, and the hash differs.