Using GPG, SSH, or S/MIME, you can sign commits and tags locally. These commits and tags are marked as verified on GitHub so other people can be confident that the changes come from a trusted source (see the full GitHub documentation here).
You should only set up one of these options - don't attempt to set up GPG and SSH commit signing!
The instructions on this page focus on the recommended method - GPG.
If you have already committed and need to retrospectively sign commits, follow the instructions below, then follow the retrospective commit signing instructions.
-
Install
gnupg&pinentry-macwith Brew:brew upgrade brew install gnupg pinentry-mac sed -i '' '/^export GPG_TTY/d' ~/.zshrc echo export GPG_TTY=\$\(tty\) >> ~/.zshrc source ~/.zshrc PINENTRY_BIN=$(whereis -q pinentry-mac) mkdir -p ~/.gnupg touch ~/.gnupg/gpg-agent.conf sed -i '' '/^pinentry-program/d' ~/.gnupg/gpg-agent.conf echo "pinentry-program ${PINENTRY_BIN}" >> ~/.gnupg/gpg-agent.conf gpgconf --kill gpg-agent
-
Create a new GPG key:
gpg --full-generate-key
-
Pick
(9) ECC (sign and encrypt)thenCurve 25519(Ed25519 offers the strongest encryption at time of writing) -
Select a key expiry time (personal choice)
-
Real name= Your GitHub handle -
Email address= An email address registered against your GitHub account - to enable Smart Commits (Jira/GitHub integration), use your@nhs.netaddressIf instead you opt for the private @users.noreply.github.com email address, consider enabling
Block command line pushes that expose my email. -
Avoid adding a comment (this may prevent git from auto-selecting a key - see Troubleshooting section below)
-
Review your inputs and press enter
Oto confirm -
Define a passphrase for the key
-
-
Test the key is visible and export the PGP public key (to your clipboard):
gpg -k # This should list the new key gpg --armor --export <my_email_address> | pbcopy
Your PGP public key is now in your clipboard!
-
Add the public key to your GitHub account (
Settings->SSH and GPG keys->New GPG key)Note the
Key IDas you'll need this in the next step. -
Set your local git config to use GPG signing:
git config --global user.email <my_email_address> # same one used during key generation git config --global user.name <github_handle> git config --global user.signingkey <key_id> git config --global commit.gpgsign true git config --global tag.gpgsign true
-
Test it works:
-
Create a temporary branch of your favourite repository.
-
Make an inconsequential whitespace change.
-
Commit the change.
- You will be prompted for your GPG key passphrase - optionally select to add it to the macOS Keychain.
-
Check the latest commit shows a successful signing:
$ git log --show-signature -1 ... gpg: Good signature from "<github_handle> <<my_email_address>>" [ultimate] Author: <github_handle> <<my_email_address>> ...
-
-
Install (as administrator) Git for Windows (which includes Bash and GnuPG)
-
Open
Git Bash -
Create a new GPG key:
gpg --full-generate-key
-
Pick
(9) ECC (sign and encrypt)thenCurve 25519(Ed25519 offers the strongest encryption at time of writing)If you already had Git for Windows installed, and its version is between
2.5.0and2.30.x, the(9) ECC and ECCoption is available if you rungpg --expert --full-generate-key, however if you can upgrade to the latest version, this is advised. -
Select a key expiry time (personal choice)
-
Real name= Your GitHub handle -
Email address= An email address registered against your GitHub account - to enable Smart Commits (Jira/GitHub integration), use your@nhs.netaddressIf instead you opt for the private @users.noreply.github.com email address, consider enabling
Block command line pushes that expose my email. -
Avoid adding a comment (this may prevent git from auto-selecting a key - see Troubleshooting section below)
-
Review your inputs and press enter
Oto confirm -
A new window called pinentry will appear prompting you to enter a passphrase.
-
-
Test the key is visible and export the PGP public key (to your clipboard):
gpg -k # This should list the new key gpg --armor --export <my_email_address> | clip
Your PGP public key is now in your clipboard!
-
Add the public key to your GitHub account (
Settings->SSH and GPG keys->New GPG key)Note the
Key IDas you'll need this in the next step. -
Set your local git config to use GPG signing:
git config --global user.email <my_email_address> # same one used during key generation git config --global user.name <github_handle> git config --global user.signingkey <key_id> git config --global commit.gpgsign true git config --global tag.gpgsign true
-
Now your key is created, make it available within Windows:
-
Export the key:
gpg --output <GitHub handle>.pgp --export-secret-key <my_email_address>
-
Install (as administrator) Gpg4win (which includes GnuPG and Kleopatra)
Ensure both
GnuPGandKleopatraare installed! -
Open Kleopatra ->
Import-> Select the<GitHub handle>.pgpfile created in the first step -
In
cmd, test the key is visible and set your local git config to use GPG signing:gpg -k # This should list the new key git config --global user.email <my_email_address> # same one used during key generation git config --global user.name <github_handle> git config --global user.signingkey <key_id> git config --global commit.gpgsign true git config --global tag.gpgsign true
-
-
Now make it available within WSL:
-
Within Ubuntu:
sudo ln -s /mnt/c/Program\ Files\ \(x86\)/GnuPG/bin/gpg.exe /usr/local/bin/gpg sudo ln -s gpg /usr/local/bin/gpg2
-
Close and reopen your Ubuntu terminal
-
Test the key is visible and set your local git config to use GPG signing:
gpg -k # This should list the new key git config --global user.email <my_email_address> # same one used during key generation git config --global user.name <github_handle> git config --global user.signingkey <key_id> git config --global commit.gpgsign true git config --global tag.gpgsign true
-
-
Test it works:
-
Create a temporary branch of your favourite repository.
-
Make an inconsequential whitespace change.
-
Commit the change.
- You will be prompted for your GPG key passphrase.
-
Check the latest commit shows a successful signing:
$ git log --show-signature -1 ... gpg: Good signature from "<github_handle> <<my_email_address>>" [ultimate] Author: <github_handle> <<my_email_address>> ...
-
A GitHub Actions workflow will by default authenticate using a GITHUB_TOKEN which is generated automatically.
However, at the time of writing, to sign commits the workflow will need to use a dedicated GitHub identity (in which to register the GPG public key).
The workflow would then use a Personal Access Token, stored with the GPG private key in the repo secrets, like so:
steps:
- name: Checkout
uses: actions/checkout@v5
with:
token: ${{ secrets.BOT_PAT }}
ref: mainExample run action script excerpt:
# Configure GPG Key for signing GitHub commits
if [ -n "${{ secrets.BOT_GPG_KEY }}" ]; then
echo "GPG secret key found in repo secrets, enabling GitHub commmit signing"
GITHUB_SIGNING_OPTION="-S"
echo "${BOT_GPG_KEY}" | gpg --import
# Highlight any expiry date
gpg --list-secret-keys
fi
git add .
git config --global user.name "${GITHUB_USER_NAME}"
git config --global user.email "${GITHUB_USER_EMAIL}"
git commit ${GITHUB_SIGNING_OPTION} -am "Automated commit from GitHub Actions: ${WORKFLOW_URL}"
git pushThe cryptographic libraries in the default Amazon Linux 2 distro are very old, and do not support elliptic curve cryptography. When using pre-existing solution elements updating the build container is not always an option. This restricts the GPG key algorithm to RSA. You should use RSA-4096, which is the required minimum for GitHub.
Furthermore, the Systems Manager Parameter Store will not accept a key that is generated for both signing and encrypting (which will contain a second key for the encryption). It will be too large to be pasted in as a valid parameter. So when generating the GPG key you must select type RSA (sign only) if you intend to use Parameter Store rather than AWS Secrets Manager.
Example AWS CodeBuild Buildspec excerpt:
# Create SSH identity for connecting to GitHub
BOT_SSH_KEY=$(aws ssm get-parameter --name "/keys/ssh-key" --query "Parameter.Value" --output text --with-decryption 2> /dev/null || echo "None")
if [[ ${BOT_SSH_KEY} != "None" ]]; then
mkdir -p ~/.ssh
echo "Host *" >> ~/.ssh/config
echo "StrictHostKeyChecking yes" >> ~/.ssh/config
echo "UserKnownHostsFile=~/.ssh/known_hosts" >> ~/.ssh/config
echo "${BOT_SSH_KEY}" > ~/.ssh/ssh_key
echo -e "\n\n" >> ~/.ssh/ssh_key
chmod 600 ~/.ssh/ssh_key
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/ssh_key
fi
gpg --version
echo
BOT_GPG_KEY=$(aws ssm get-parameter --name "/keys/gpg-key" --query "Parameter.Value" --output text --with-decryption 2> /dev/null || echo "None")
if [ "${BOT_GPG_KEY}" != "None" ]; then
echo "Encrypted GPG secret key found in the Parameter Store, enabling GitHub commmit signing"
GITHUB_SIGNING_OPTION="-S"
echo "${BOT_GPG_KEY}" | gpg --import
# Highlight any expiry date
gpg --list-secret-keys
gpg-agent --daemon
echo
fi
# GitHub publishes its public key fingerprints here:
# https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/githubs-ssh-key-fingerprints
# The known_hosts entry below was obtained by validating the fingerprint on a separate computer
echo "github.com ssh-ed25519 ${GITHUB_FINGERPRINT}" >> ~/.ssh/known_hosts
git config --global advice.detachedHead false
git config --global user.name "${GITHUB_USER_NAME}"
git config --global user.email "${GITHUB_USER_EMAIL}" # same one used during key generation
git clone git@github.com:${GITHUB_ORG_NAME}/${GITHUB_REPO_NAME}.git
# Make git repository source code changes here
git add .
git commit ${GITHUB_SIGNING_OPTION} -am "Automated commit from ${SCRIPT_URL}"
git pushRe-run your git command prefixed with GIT_TRACE=1.
A failure to sign a commit is usually because the name or email does not quite match those which were used to generate the GPG key, so git cannot auto-select a key. Ensure that these are indeed consistent. (If you added a comment when creating your GPG key, this may cause a mismatch: the comment will be visible when listing your GPG keys, e.g. RealName (Comment) <EmailAddress>.) You are able to force a choice of signing key, though this should not be necessary.