This guide shows how to generate a GPG key, configure Git to use it, and add it to GitHub so your commits can be verified.
- A GitHub account with a verified email address.
- Git installed on your computer.
- GPG installed on your computer.
GitHub recommends verifying your email before generating a key, because the email in your GPG key must match the identity you use for Git commits [web:4][web:1].
brew install gnupgsudo apt update
sudo apt install gnupgInstall Gpg4win from the official website.
Open Terminal or Git Bash and run:
gpg --full-generate-keyGitHub’s documentation also supports gpg --generate-key for newer GPG versions, but --full-generate-key gives you more control over the options [web:2][web:4].
When prompted:
- Choose
RSA and RSAif available. - Use a key size of
4096. - Set an expiration date if desired.
- Enter your full name.
- Enter the email address linked to your GitHub account.
- Create a strong passphrase.
After creating the key, list your secret keys:
gpg --list-secret-keys --keyid-format=longLook for the key ID after sec. You will need it for Git configuration [web:1].
Copy your public key in ASCII format:
gpg --armor --export YOUR_KEY_IDReplace YOUR_KEY_ID with the long key ID from the previous step. GitHub asks you to copy the block beginning with -----BEGIN PGP PUBLIC KEY BLOCK----- and ending with -----END PGP PUBLIC KEY BLOCK----- [web:4][web:7].
- Go to GitHub and open Settings.
- In the sidebar, select SSH and GPG keys.
- Under GPG keys, click New GPG key.
- Add a title for the key.
- Paste the public key.
- Click Add GPG key.
- Complete any authentication prompt if shown [web:7].
Tell Git which key to use for signing:
git config --global user.signingkey YOUR_KEY_IDIf you want Git to sign every commit automatically:
git config --global commit.gpgsign trueGitHub documents this global signing setting for automatic commit signing [web:1][web:3].
Create or amend a commit, then verify that Git signs it:
git commit -S -m "My signed commit"If global signing is enabled, you can also use a normal commit command:
git commit -m "My signed commit"GitHub should show the commit as verified after the key is correctly added and the commit email matches your GitHub email [web:1][web:6].
- Make sure the email in the key matches your GitHub account email.
- Make sure Git is using the same key you added to GitHub.
- If your commit is not verified, confirm
commit.gpgsignis enabled or use-S. - If GitHub does not show the key, paste the full public key block again.
gpg --list-secret-keys --keyid-format=long
gpg --armor --export YOUR_KEY_ID
git config --global user.signingkey YOUR_KEY_ID
git config --global commit.gpgsign true
git commit -S -m "Signed commit"By default, GPG stores its configuration, public keyring, and private keys in:
~/.gnupgOn some systems, this location can be changed with:
GNUPGHOMEgpg --homedir /path/to/dir
Secret keys are stored in the GnuPG private key area inside that home directory [web:26][web:28][web:30].
- Public keys and keyring files:
~/.gnupg - Secret key material:
~/.gnupg/private-keys-v1.d - Configuration files:
~/.gnupg/gpg.confand related files [web:28][web:30].
Show all public keys:
gpg --list-keysShow all secret keys:
gpg --list-secret-keys --keyid-format=longUse these commands whenever you need to confirm which keys exist on your machine [web:27][web:30].
gpg --delete-secret-key YOUR_KEY_IDgpg --delete-key YOUR_KEY_IDGnuPG expects the secret key to be removed first if it still exists, and will prompt you accordingly [web:27][web:21].
To remove a key from your GitHub account, go to:
- GitHub Settings
- SSH and GPG keys
- GPG keys
- Remove the key you no longer want
You can also use the GitHub CLI:
gh gpg-key delete <key-id>Deleting the key from GitHub affects verification on GitHub, while deleting it locally removes it from your computer [web:23][web:31].
Before (deleting a key)[https://www.gnupg.org/gph/en/manual/r1045.html], consider revoking it if it may have been compromised. Revocation is safer than simple deletion because it tells others the key should no longer be trusted.
If your key is compromised, lost, or no longer needed, revoke it instead of only deleting it.
gpg --output revoke.asc --gen-revoke YOUR_KEY_IDSave this file somewhere secure. Anyone with this certificate can revoke your key, so do not store it in an unsafe place [web:15][web:47].
gpg --import revoke.ascThis marks the key as revoked in your local keyring [web:17][web:39].
gpg --armor --export YOUR_KEY_ID > revoked-public-key.ascDelete the old GPG key from GitHub Settings → SSH and GPG keys → GPG keys. GitHub community guidance notes that some users then re-add the revoked public key so GitHub can display the revoked or expired state consistently [web:34][web:17].
Generate a new GPG key and update Git to use it for future signed commits.