This guide explains how to configure GPG commit signing on macOS using Homebrew, ensuring your passphrase saves securely to the macOS Keychain to eliminate constant password prompts.
Install GPG and the macOS pinentry graphical application via Homebrew:
brew install gnupg pinentry-mac-
Find the absolute path of your
pinentry-macinstallation:which pinentry-mac
(Usually
/opt/homebrew/bin/pinentry-macon Apple Silicon or/usr/local/bin/pinentry-macon Intel). -
Create or overwrite your GPG agent configuration file (
~/.gnupg/gpg-agent.conf) using that absolute path:# Replace the path below if your 'which pinentry-mac' output was different echo "pinentry-program /opt/homebrew/bin/pinentry-mac" > ~/.gnupg/gpg-agent.conf echo "default-cache-ttl 604800" >> ~/.gnupg/gpg-agent.conf echo "max-cache-ttl 604800" >> ~/.gnupg/gpg-agent.conf
-
Add the GPG TTY variable to your shell profile (
~/.zshrcor~/.bash_profile):echo 'export GPG_TTY=\$(tty)' >> ~/.zshrc source ~/.zshrc
-
Find your long GPG key ID:
gpg --list-secret-keys --keyid-format LONG
Look for the line starting with
sec. The ID is the alphanumeric string right after the slash (e.g.,3AA5C34371567BD2). -
Link your key to Git and enable global automatic signing:
git config --global user.signingkey YOUR_KEY_ID_HERE git config --global commit.gpgsign true git config --global gpg.program \$(which gpg)
-
Force the GPG agent to reload and read the new configurations:
gpgconf --kill gpg-agent gpg-connect-agent reloadagent /bye
-
Trigger the Keychain prompt by making a test commit:
git commit -m "test: verify automatic gpg signing" -
When the native macOS pop-up window appears, enter your GPG passphrase and check the box to save it in your Keychain.
- Verify Signature: Run
git log --show-signature -1to confirm the commit shows aGood signature. - Fix "No pinentry" Error: If signing fails, verify that the path inside
~/.gnupg/gpg-agent.confmatches your actualwhich pinentry-macpath, then run the reload commands in Step 3 again.