This guide shows you how to set up GPG signing for your Git commits. Use this to cryptographically sign your commits, providing verification of authorship.
Ensure you have:
brew install gnupg)Skip this if you already have a GPG key.
gpg --full-generate-key
(1) RSA and RSA40962y (2 years recommended)gpg --list-secret-keys --keyid-format=long
Output example:
sec rsa4096/56BCDEBC6448A091 2024-01-01 [SC]
E8404D8E8DAB59CD1E5255BD56BCDEBC6448A091
uid [ultimate] Your Name <your.email@example.com>
This repository uses a secure split configuration pattern to keep your GPG key private:
.gitconfig - Public configuration (tracked in repo).gitconfig.local.template - Template showing the format (tracked).gitconfig.local - Your private configuration (NOT tracked)# After running stow git
cd ~
cp .gitconfig.local.template .gitconfig.local
$EDITOR ~/.gitconfig.local
Update with your actual GPG key:
[user]
signingKey = E8404D8E8DAB59CD1E5255BD56BCDEBC6448A091
.gitconfig includes this at the top:
[include]
path = ~/.gitconfig.local
This loads your local settings, overriding any values in the main config.
.gitconfig):
[commit]
gpgsign = true
mkdir -p ~/.gnupg
cat > ~/.gnupg/gpg-agent.conf << EOF
default-cache-ttl 600
max-cache-ttl 7200
pinentry-program /opt/homebrew/bin/pinentry-mac
enable-ssh-support
EOF
gpgconf --kill gpg-agent
gpg-agent --daemon
Add to your shell configuration (already in the dotfiles):
# Check zsh/.zshrc includes:
export GPG_TTY=$(tty)
cd ~/test-repo
echo "test" > test.txt
git add test.txt
git commit -m "test: GPG signed commit"
git log --show-signature -1
You should see:
gpg: Good signature from "Your Name <email>"
brew install pinentry-mac
echo "use-agent" >> ~/.gnupg/gpg.conf
echo "pinentry-mode loopback" >> ~/.gnupg/gpg.conf
To use your GPG key for SSH:
gpg --list-keys --with-keygrip
echo "[KEYGRIP]" >> ~/.gnupg/sshcontrol
export SSH_AUTH_SOCK=$(gpgconf --list-dirs agent-ssh-socket)
# Verify signing is enabled
git config --get commit.gpgsign
# Verify signing key
git config --get user.signingKey
# Create a signed commit
git commit -S -m "test: signed commit"
# Verify signatures in log
git log --show-signature
Problem: “error: gpg failed to sign the data”
Solution:
export GPG_TTY=$(tty)gpgconf --kill gpg-agentgpg --list-keysProblem: No pinentry dialog appears
Solution:
brew install pinentry-macProblem: “No secret key” error
Solution:
gpg --list-secret-keysgpg --import private-key.ascProblem: Commits show as “Unverified” on GitHub
Solution:
gpg --armor --export YOUR_KEY_ID