How do you configure Git to use a specific GPG key for signing commits?
Posted by MaryJns
Last Updated: July 22, 2024
To configure Git to use a specific GPG key for signing commits, you need to follow these steps: 1. Find your GPG Key ID: First, you need to identify the GPG key you want to use. You can list your keys with the following command:
gpg --list-secret-keys --keyid-format LONG
This will output something like this:
/home/user/.gnupg/secring.gpg
   ------------------------------
   sec   4096R/1234567890ABCDEF 2021-01-01 [expires: 2023-01-01]
   uid                          Your Name <you@example.com>
   ssb   4096R/0987654321FEDCBA 2021-01-01
In this case, 1234567890ABCDEF is the key ID that you will use. 2. Configure Git to use your GPG key: Use the following command to set your GPG key as the default for signing Git commits. Replace 1234567890ABCDEF with your actual GPG key ID.
git config --global user.signingkey 1234567890ABCDEF
3. Enable commit signing by default: If you want all your commits to be signed by default, you can configure this with:
git config --global commit.gpgSign true
4. Ensure GPG is correctly configured: Make sure that your GPG agent is set up correctly and that Git can find your GPG installation. You can test whether GPG signing works by running:
git commit -S -m "Your commit message"
If prompted for a passphrase, enter your GPG key's passphrase. 5. Use GPG TTY if necessary: On some systems, particularly when using a GUI like Git GUI or in IDEs, you might encounter issues with GPG not being able to prompt for the passphrase. You can set the GPG TTY environment variable to fix this. Add the following line to your shell configuration file (like .bashrc, .zshrc, etc.):
export GPG_TTY=$(tty)
6. Test your Setup: