Sub topic 3
Creating a GPG key and finding its ID
This chapter assumes you can commit, tag, and run git config, that you are comfortable in a shell, and that you know what a public and private key are. You also need GnuPG installed. Check with gpg --version.
By the end you will have a GPG key pair and know the exact ID that Git needs in user.signingkey.
What you are creating
GnuPG (GPG) is the program that implements OpenPGP. When you create a key, GPG makes a key pair. The private key stays on your machine and produces signatures. The public key is shared so others can check them.
A GPG key also carries a user ID, which is a name and email address. Hosting platforms and Git compare that email against the committer email, so use an address you actually commit with.
Generate the key
Run the interactive generator:
gpg --full-generate-keyUse --full-generate-key rather than --generate-key. The short form skips the questions and picks defaults for you. The full form lets you choose the following.
| Prompt | Recommended answer | Reason |
|---|---|---|
| Key kind | ECC (sign and encrypt), or "ECC (sign only)" if offered | Ed25519 keys are small and fast |
| Curve | Curve 25519 | Widely supported by current GnuPG |
| Expiry | `1y` or `2y` | An expiry limits the damage from a lost key and can be extended later |
| Real name and email | Your name and your commit email | The email must match what Git records |
| Passphrase | A strong passphrase | Protects the private key if the file is copied |
If your GnuPG version is older and has no ECC option, choose RSA with a key size of 4096 bits.
Find the key ID
Git needs to know which key to use. List your secret keys in the long format:
gpg --list-secret-keys --keyid-format=longThe output looks like this:
sec ed25519/3AA5C34371567BD2 2026-01-01 [SC] [expires: 2027-01-01]
9F2C1B7E4D8A0C55E31A6B2F3AA5C34371567BD2
uid [ultimate] Alice Example <[email protected]>
ssb cv25519/7C1D9E02B4F6A388 2026-01-01 [E]Read the lines as follows:
secis the primary secret key. The text after the slash,3AA5C34371567BD2, is the long key ID. This is the value you give to Git.- The line beneath it is the full 40-character fingerprint. The long ID is its last 16 characters.
[SC]means the key can sign and certify. Signing is the capability Git needs.uidis the name and email attached to the key.ssbis a subkey. Here it is for encryption ([E]), so Git does not use it.
The long format matters because short 8-character IDs are easy to forge with collisions. Always copy the long ID or the full fingerprint.
Hand the ID to Git
The next chapter covers configuration in full. For now, this is the minimum that connects your key to Git:
git config --global user.signingkey 3AA5C34371567BD2Replace the example ID with your own.
Export the public key
A platform or colleague needs your public key to verify signatures. Export it in ASCII form:
gpg --armor --export 3AA5C34371567BD2The output starts with -----BEGIN PGP PUBLIC KEY BLOCK-----. Copy the whole block, including those lines. Never export with --export-secret-keys for this purpose, since that outputs your private key.
Common mistakes
- Using an email that differs from
git config user.email. Signatures then show as unverified on hosting platforms. - Copying the key ID from the
ssbline. That is a subkey, not the primary key. - Running a command against the wrong key because several keys exist. Check the
uidline before you copy an ID.
Exercise
You run gpg --list-secret-keys --keyid-format=long and see the line sec ed25519/3AA5C34371567BD2 2026-01-01 [SC]. Which value do you pass to git config user.signingkey, and why use the long format rather than the default short ID?
Show answer
Pass 3AA5C34371567BD2, the text after the slash on the sec line. The long format shows 16 characters of the fingerprint, which is much harder to forge by collision than an 8-character short ID.
Exercise
Why does the email in the key's user ID matter for Git signing?
Show answer
Hosting platforms match the key's email against the commit's committer email. If they differ, the signature cannot be shown as verified.