Sub topic 11
Passphrases, agents, and hardware-backed keys
This chapter assumes you have already created a signing key and set commit.gpgsign, as covered earlier. It explains how Git gets access to the private key without asking for the passphrase on every commit. It also covers the usual failures and what hardware keys change.
Why a passphrase matters
A signing key without a passphrase is a file that anyone who copies it can use to sign as you. A passphrase encrypts the private key on disk. The cost is a prompt each time Git needs the key. An agent removes most of that cost. An agent is a background process that holds the unlocked key in memory and signs on request, so the key file is decrypted once.
How Git reaches the key
Git never reads your private key itself. It runs an external program and passes it the data to sign.
| Format | Program Git runs | Agent that holds the key |
|---|---|---|
| OpenPGP (`gpg.format = openpgp`) | `gpg` | `gpg-agent` |
| SSH (`gpg.format = ssh`) | `ssh-keygen -Y sign` | `ssh-agent` |
GPG: pinentry and GPG_TTY
gpg-agent asks for the passphrase through a program called pinentry. A terminal pinentry needs to know which terminal to draw on. Git runs gpg without a terminal of its own, so the prompt can fail and you see failed to sign the data. Tell GnuPG your terminal:
export GPG_TTY=$(tty)
echo "test" | gpg --clearsignAdd the export line to your shell startup file, such as ~/.bashrc or ~/.zshrc. If the test command succeeds, GnuPG works and the problem is in your Git configuration.
You can control how long the agent remembers the passphrase in ~/.gnupg/gpg-agent.conf:
default-cache-ttl 3600
max-cache-ttl 28800default-cache-ttl is the idle time in seconds before the passphrase is forgotten. max-cache-ttl is the absolute limit. Reload the agent with gpgconf --kill gpg-agent. It restarts on the next use.
SSH: ssh-agent
For SSH signing, add the key to the agent once per session:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519_signing
ssh-add -lssh-add -l lists loaded keys. Many desktops and macOS start an agent for you. If ssh-add -l reports it cannot connect, no agent is running or SSH_AUTH_SOCK is not set.
Git can also sign with a key that exists only in the agent. Set user.signingkey to the public key text prefixed with key::, or point to the .pub file. Git then asks the agent to sign.
Hardware-backed keys
A hardware key, such as a YubiKey or a smart card, stores the private key on the device. The key cannot be copied off it. Signing happens inside the device, often after a PIN or a touch.
- SSH: Generate a FIDO2 key with
ssh-keygen -t ed25519-sk. The-sktypes need OpenSSH 8.2 or later, and Git signing needs Git 2.34 or later. The file on disk is only a handle. Without the device it is useless. - GPG: Move a subkey to an OpenPGP smart card with
gpg --edit-key, thenkeytocard. GnuPG talks to the card throughscdaemon.
Expect a touch or PIN for each signature. That is the security benefit, and it also makes rebases of many commits slow, because each signed commit asks again.
Choosing a setup
| Situation | Reasonable choice |
|---|---|
| Personal laptop, daily use | Passphrase plus agent with a cache timeout |
| Shared or remote machine | Hardware key, so the private key never reaches the machine |
| CI or automation | A dedicated key limited to that job, stored in the CI secret store |
Exercise
You run git commit in your terminal and get error: gpg failed to sign the data. The key and the Git configuration are correct. What do you check first, and what command isolates the problem from Git?
Show answer
Check that GPG_TTY is set, so pinentry can prompt on your terminal. Run export GPG_TTY=$(tty) and then echo "test" | gpg --clearsign. If that works, retry the commit. If it still fails, the problem is in GnuPG or the Git configuration, not the prompt.
Exercise
Why does an SSH-signed commit fail inside an IDE even though it works in your terminal, when your key has a passphrase?
Show answer
The IDE has no terminal for ssh-keygen to prompt on, and the key is not loaded in an agent. Run ssh-add on the key in a terminal first, and make sure the IDE sees the same SSH_AUTH_SOCK.