Auto-signing Git commits using GPG

Signed commits allow others to trust that changes you've made really are made by you.

To acheive this using GPG you will need an existing key, or to generate a new one, and to configure your GitHub account with your new key. Its also a massive life saver to auto-sign commits, as typing passwords every 10 minutes isn't much fun!

Install GPG and GPG Agent packages

On OSX, gpg and gpg-agent can be installed using Homebrew:

brew install gpg gpg-agent

Generate a GPG key

To generate a new key, enter the following gpg command into a terminal, along with your real name, email and passphrase when prompted. (NB: Use a unique password here and not one associated with any other account, as this passphrase will be stored later within your .bashrc file)

gpg --default-new-key-algo rsa4096 --gen-key

Adding the GPG key to your GitHub account

Following instructions on the following link, will guide to you on how to export your public key for use with your github account.

Updating .bashrc to preset your passphrase

In order to benefit from auto-signed commits without a passphrase prompt, we'll need to preset our passphrase with the gpg-agent which caches it for later use and allowing github commits to function seamlessly.

The following code to be pasted into your ~/.bashrc file, will set your git config with a signing key, remove any existing gpg-agent processes and then finally start a new gpg-agent with your passphrase preset:

# github auto-signed commits
export GNUPGPATH=/usr/local/Cellar/gnupg/2.2.9/libexec
export GPG_TTY=$(tty)

git config --global user.signingkey $GPG_SIGNING_KEY
git config --global commit.gpgsign true
pkill gpg-agent
eval $(gpg-agent --daemon --allow-preset-passphrase --default-cache-ttl 1 --max-cache-ttl 31536000)
gpg-preset-passphrase -P "$GPG_PASSPHRASE" --preset $GPG_PRESET


Replace <ADD_GPG_PASSPHRASE_HERE> in the .bashrc file with your unique passphrase.


In a terminal, enter:

gpg --fingerprint --list-secret-keys --with-keygrip --keyid-format LONG


We can find the signing key after rsa4096/ (e.g 92E95393EDB7921F above), and use this value to replace <ADD_GPG_SIGNING_KEY_HERE> in the .bashrc file.

Also the Keygrip value (e.g 8797FD2EAABD7314AEEB0D71F46B22619508E672) can be found above the key created using your name/email, and it should replace <ADD_GPG_PRESET_HERE> in the .bashrc file.


Save your .bashrc file and close/reopen your bash terminal, and navigate to a repository and commit changes with the -S flag.

git commit -S -am "Testing GH signed commit"

..and your commits should now be auto-signed, and trusted!