Sub topic 3
Creating a GPG key and finding its ID
This chapter assumes you know basic Git (commit, tag, log, git config), can use a shell, and understand public and private keys. It covers generating an OpenPGP key with GnuPG, reading the listing output, and picking the identifier Git needs. Later chapters wire the key into Git and verify signatures.
What you are creating
A GPG key is a bundle of keys under one identity. GnuPG calls the main key the primary key. It usually carries two capabilities:
- S (sign): creates signatures. Git uses this one.
- C (certify): vouches for other keys and user IDs, and binds subkeys to the primary key.
Two other capabilities exist. E (encrypt) is for encrypting data. A (authenticate) is for logging in. Modern GnuPG puts E on a separate subkey, so a key listing shows a primary key plus one or more subkeys.
The block above is only a placeholder-free outline. The real diagram follows.
The user ID (UID) is a name and email pair attached to the key. It matters for Git. Hosting platforms and git log --show-signature compare the email in the UID with the committer email, so a mismatch produces an unverified result later.
Check your GnuPG version
Run this first:
gpg --versionUse GnuPG 2.1 or later. Ed25519 keys need 2.1.17 or later. On older systems, gpg may be GnuPG 1.x and gpg2 the modern one. Git runs the program named by gpg.program, so make sure you generate the key with the same binary Git will call.
Generate a key interactively
Use the full generator, which exposes every option:
gpg --full-generate-keyGnuPG asks a series of questions:
- Kind of key. Choose
(9) ECC (sign and encrypt)or(10) ECC (sign only). The numbers differ between versions, so read the menu. ECC keys are small and fast. RSA 4096 is the fallback if a tool you use does not accept ECC. - Curve. Choose
Curve 25519. This produces an Ed25519 signing key, plus a Curve25519 encryption subkey if you chose "sign and encrypt". - Expiry. Enter a value such as
2y. An expiry is a safety net, because a lost key stops being valid by itself. You can extend it later without changing the key ID. - Real name and email. Enter the exact email address you set in
git config user.email. - Comment. Leave it empty. Comments in a UID mostly cause confusion.
- Passphrase. A pinentry dialog asks for one. Use a strong passphrase. It is the main protection if the private key file is copied.
GnuPG then gathers entropy and creates the key. It also writes a revocation certificate to ~/.gnupg/openpgp-revocs.d/. Copy that file to offline storage now. It is the only way to revoke the key if you lose access to it.
Generate a key non-interactively
For scripts or a quick setup, --quick-generate-key takes the UID, algorithm, usage, and expiry as arguments:
gpg --quick-generate-key "Alice Example <[email protected]>" ed25519 sign 2yThis creates a signing-only Ed25519 primary key valid for two years. GnuPG still asks for a passphrase through pinentry. It has no encryption subkey, which is fine if you only sign commits. Add one later with gpg --quick-add-key FINGERPRINT cv25519 encr 2y if you need it.
List keys and read the output
List your secret keys with long key IDs:
gpg --list-secret-keys --keyid-format=longThe output looks like this:
/home/alice/.gnupg/pubring.kbx
------------------------------
sec ed25519/3AA5C34371567BD2 2026-01-01 [SC] [expires: 2028-01-01]
9F2C1B7E4D8A60C3B15E7A903AA5C34371567BD2
uid [ultimate] Alice Example <[email protected]>
ssb cv25519/8D1E2F4A6B7C9D0E 2026-01-01 [E] [expires: 2028-01-01]Read it line by line:
| Part | Meaning |
|---|---|
| `sec` | A secret primary key. `ssb` is a secret subkey. |
| `ed25519/3AA5C34371567BD2` | Algorithm and the 16-hex-digit long key ID. |
| `[SC]` | The key can sign and certify. |
| 40-hex line | The full fingerprint of the primary key. |
| `uid ... [ultimate]` | The name and email, with your local trust level. Keys you created yourself show as ultimate. |
| `ssb ... [E]` | A subkey that can only encrypt. |
Which identifier to give Git
GnuPG shows a key in three forms:
| Form | Length | Use it? |
|---|---|---|
| Short ID | 8 hex digits | No. Collisions are easy to produce on purpose. |
| Long ID | 16 hex digits | Acceptable. This is what the example in the Git docs uses. |
| Fingerprint | 40 hex digits | Best. It identifies exactly one key. |
The long ID is the last 16 digits of the fingerprint. In the listing above, 3AA5C34371567BD2 is the tail of 9F2C1B7E4D8A60C3B15E7A903AA5C34371567BD2.
Git passes the value of user.signingkey to GnuPG as the key to sign with. Anything GnuPG can resolve works, including a fingerprint or an email address. A fingerprint is the least ambiguous choice. An email can match several keys and make GnuPG pick one for you.
Capture the ID in a script
Copying IDs by hand invites typos. The --with-colons format is stable and meant for scripts. The fpr record's tenth field is the fingerprint, and the first fpr line belongs to the primary key:
KEYID=$(gpg --list-secret-keys --with-colons [email protected] \
| awk -F: '$1 == "fpr" { print $10; exit }')
echo "$KEYID"
git config --global user.signingkey "$KEYID"The next chapter covers the Git configuration in full. The last line is shown here so you can see where the ID ends up.
If your key has a separate signing subkey and you want Git to use it, append an exclamation mark to the ID, as in git config --global user.signingkey 8D1E2F4A6B7C9D0E!. The ! tells GnuPG to use exactly that subkey and not choose one itself.
Check that the email matches
Signature verification on a hosting platform requires two things. The key's UID must contain the email on the commit, and the platform must know that email as verified on your account. Check the email Git will record:
git config user.email
gpg --list-keys --keyid-format=long [email protected]If you need another address on the same key, add a UID:
gpg --edit-key [email protected]At the gpg> prompt, type adduid, answer the name and email questions, then type save. Adding a UID changes the public key, so you must export it again and upload it to any platform that holds the old copy.
Export the public key
Hosting platforms need your public key, never the private one. Export it in ASCII-armored form so it can be pasted into a web form:
gpg --armor --export 9F2C1B7E4D8A60C3B15E7A903AA5C34371567BD2The output begins with -----BEGIN PGP PUBLIC KEY BLOCK-----. Copy the whole block, including those lines. Display the fingerprint in grouped form with gpg --fingerprint [email protected] when you need to compare it by eye with another copy.
Back up the key
Your private key lives in ~/.gnupg/private-keys-v1.d/. If the machine dies, the key goes with it, and old commits can no longer be tied to a key you control. Export a backup and store it somewhere offline:
gpg --armor --export-secret-keys 9F2C1B7E4D8A60C3B15E7A903AA5C34371567BD2 > alice-secret.asc
chmod 600 alice-secret.ascCommon mistakes
- Using the wrong key listing.
gpg --list-keysshows public keys.gpg --list-secret-keysshows keys you can sign with. If a key appears only in the first, the secret half is missing. - Mixing up the primary key ID and the subkey ID. Signing uses the key marked
S. Copy thesecline for a primary key marked[SC], not thessbline marked[E]. - Using an email that differs from
user.email. The key works, but signatures show as unverified on the platform. - Skipping the expiry. A key with no expiry stays valid forever if it leaks and you cannot revoke it. Set one and extend it as needed.
- Generating with a different
gpgbinary than Git uses. Checkgit config gpg.programif the key you made is not found.
Exercise
A teammate pastes gpg --list-secret-keys --keyid-format=long output showing sec rsa4096/AB12CD34EF56AB78 ... [SC], a 40-digit fingerprint line, and ssb rsa4096/99887766AABBCCDD ... [E]. Which identifier should they give to user.signingkey, and why not the other one?
Show answer
They should use the fingerprint from the sec entry, or at least AB12CD34EF56AB78. That key has the S capability, and the fingerprint identifies exactly one key. The ssb ID 99887766AABBCCDD belongs to a subkey marked E, which can only encrypt, so GnuPG cannot sign with it. They should also avoid any 8-digit short ID because short IDs can collide.
Exercise
You created a key with the email [email protected], but git config user.email returns [email protected]. Commits you sign will show as unverified on the hosting platform. Name two ways to fix this.
Show answer
Either change Git's email for the repository with git config user.email [email protected], or add the work address to the key with gpg --edit-key, adduid, save, then export the updated public key and upload it again. In both cases the email must also be verified on the hosting account.