This ultimate guide walks you through generating a GPG key on Linux, linking it to GitHub, and signing commits to earn that shiny Verified badge. With detailed steps, essential commands, and stylish Markdown formatting, youโll be a GPG pro in no time! ๐
GPG keys cryptographically sign your commits, proving youโre the real deal. Hereโs why itโs awesome:
Before diving in, ensure you have:
git --version.gpg --version.Install GPG if missing:
sudo apt update && sudo apt install gnupg
Letโs create a modern, secure GPG key using ECC (Elliptic Curve Cryptography).
gpg --full-generate-key
9 for ECC (sign and encrypt) โ itโs faster and Enter.1 for Curve 25519 (Ed25519) โ the gold standard for signing.Enter.1y for 1 year or 0 for no expiration (not recommended).Enter.O (Okay).You need the key ID to configure Git and GitHub.
gpg --list-secret-keys --keyid-format=long
sec ed25519/AB1234567890CDEF 2025-05-18 [SC]
Key fingerprint = 1234 5678 90AB CDEF 1234 5678 90AB CDEF 1234 5678
uid Your Name <you@example.com>
ssb cv25519/1234567890ABCDEF 2025-05-18 [E]
AB1234567890CDEF (after ed25519/).GitHub needs your public key to verify your signed commits.
gpg --armor --export AB1234567890CDEF
-----BEGIN PGP PUBLIC KEY BLOCK-----
mQENBF...
-----END PGP PUBLIC KEY BLOCK-----
Copy the entire block, including -----BEGIN and -----END.
gpg --armor --export AB1234567890CDEF > mypublickey.asc
Set up Git to sign all commits with your GPG key.
git config --global user.signingkey AB1234567890CDEF
git config --global commit.gpgsign true
git config --global --list
Look for:
user.signingkey=AB1234567890CDEF
commit.gpgsign=true
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
Time to test your setup with a sample repo!
mkdir test-gpg && cd test-gpg
git init
echo "# Test GPG Signing" > README.md
git add README.md
git commit -m "My first signed commit ๐"
git log --show-signature -1
You should see:
commit abc123...
gpg: Signature made Sun May 18 12:51:00 2025 +0600
gpg: using ED25519 key AB1234567890CDEF
gpg: Good signature from "Your Name <you@example.com>" [ultimate]
git remote add origin https://github.com/yourusername/test-gpg.git
git branch -M main
git push -u origin main
Keep your keys safe and ready for future use.
gpg --export-secret-keys --armor AB1234567890CDEF > myprivatekey.asc
Warning: Store this file in a secure location.
gpg --import myprivatekey.asc
gpg --edit-key AB1234567890CDEF
At gpg> prompt:
expire
Set a new date, then:
save
Update GitHub with the new public key:
gpg --armor --export AB1234567890CDEF
gpg --generate-revocation AB1234567890CDEF > revoke.asc
Import if needed:
gpg --import revoke.asc
gpg --list-secret-keys --keyid-format=long.git config --global user.signingkey.git config --global user.email.gpg-agent:
sudo apt install gpg-agent
| Task | Command |
|---|---|
| Install GPG | sudo apt update && sudo apt install gnupg |
| Generate key | gpg --full-generate-key |
| List keys | gpg --list-secret-keys --keyid-format=long |
| Export public key | gpg --armor --export YOUR_KEY_ID |
| Export private key | gpg --export-secret-keys --armor YOUR_KEY_ID > myprivatekey.asc |
| Configure Git | git config --global user.signingkey YOUR_KEY_IDgit config --global commit.gpgsign true |
| Set Git user | git config --global user.name "Your Name"git config --global user.email "you@example.com" |
| Test commit | git commit -m "My signed commit" |
| Verify commit | git log --show-signature -1 |
| Extend expiration | gpg --edit-key YOUR_KEY_ID then expire |
| Revoke key | gpg --generate-revocation YOUR_KEY_ID > revoke.asc |
Youโre now a GPG master! Your commits will glow with Verified badges, and your workflow is secure as Fort Knox. Keep rocking Git, and explore more GPG tricks for emails or file encryption.
โ Your Git/GPG Sidekick
Happy coding! โจ