Skip to content

Instantly share code, notes, and snippets.

@bmhatfield
Last active March 18, 2024 07:43
Show Gist options
  • Save bmhatfield/cc21ec0a3a2df963bffa3c1f884b676b to your computer and use it in GitHub Desktop.
Save bmhatfield/cc21ec0a3a2df963bffa3c1f884b676b to your computer and use it in GitHub Desktop.
Automatic Git commit signing with GPG on OSX
# In order for gpg to find gpg-agent, gpg-agent must be running, and there must be an env
# variable pointing GPG to the gpg-agent socket. This little script, which must be sourced
# in your shell's init script (ie, .bash_profile, .zshrc, whatever), will either start
# gpg-agent or set up the GPG_AGENT_INFO variable if it's already running.
# Add the following to your shell init to set up gpg-agent automatically for every shell
if [ -f ~/.gnupg/.gpg-agent-info ] && [ -n "$(pgrep gpg-agent)" ]; then
source ~/.gnupg/.gpg-agent-info
export GPG_AGENT_INFO
else
eval $(gpg-agent --daemon --write-env-file ~/.gnupg/.gpg-agent-info)
fi
# Enables GPG to find gpg-agent
use-standard-socket
# Connects gpg-agent to the OSX keychain via the brew-installed
# pinentry program from GPGtools. This is the OSX 'magic sauce',
# allowing the gpg key's passphrase to be stored in the login
# keychain, enabling automatic key signing.
pinentry-program /usr/local/bin/pinentry-mac
# Uncomment within config (or add this line)
use-agent
# This silences the "you need a passphrase" message once the passphrase handling is all set.
# Use at your own discretion - may prevent the successful interactive use of some operations.
# It is working fine for my use cases though.
batch
# A quick outline of what must be done to get everything working.
# 1) Install the dependencies.
brew install gnupg gpg-agent pinentry-mac
# 2) Configure git to automatically gpgsign commits. This consists of
# pointing git to your signing key ID, and then enabling commit
# automatic signing.
git config --global user.signingkey <YOUR-SIGNING-KEY-PUB-ID>
git config --global commit.gpgsign true
# 3) Configure the GPG components (see above for relevant examples):
# ~/.gnupg/gpg.conf
# ~/.gnupg/gpg-agent.conf
# 4) Start the daemon and configure your shell (see above for example in .profile).
# ~/.bash_profile | ~/.zshrc
# Don't forget to upload your public key to Github!
# https://github.com/blog/2144-gpg-signature-verification
# Note: There needs to be a three-way match on your email for Github to show
# the commit as 'verified': The commit email, github email, & the email associated with the public key
# Learn about creating a GPG key and the knowledge behind these commands here:
# https://git-scm.com/book/en/v2/Git-Tools-Signing-Your-Work
@aneta-s
Copy link

aneta-s commented Dec 3, 2021

How to check what is gpg key?.
My gpg failed to sign the data after git commit -m 'sample text'

I want to push my changes to GitHub in Git command, integrated terminal in VSC, and my profile is Bash, customized in ZSH. I'm deploying to Netlify through continuous deployment from Github.
Every time I do "git commit -m "xyz', I get error:

husky > pre-commit (node v14.17.5)
⚠ Some of your tasks use `git add` command. Please remove it from the config since all modifications made by tasks will be automatically added to the git commit index.

ℹ No staged files match any configured task.
error: gpg failed to sign the data
fatal: failed to write commit object

We think it is an authentication issue, related to gpg key. We're able to run gpg2, but not able to sign in with key. We're trying to set the program to gpg2, and we have upgraded to gpg2, but it says it is already installed gpg. How to check what is gpg key?
We are not sure where to look/what to do now.
Previously I had problems with visibility of my contributions to Gihub, so I have gained Netlify permission to access my repository code. Netlify does this by installing the Netlify GitHub App on my Github account. Everything worked perfect, until I have updated my Macbook Air from Mojave to Big Sur v11. In the same time I had to update my terminal, so I screwed Homebrew upgrade. Yet, my profile is just Bash. Not sure about the last one, sorry! Not least, but last, Netlify just announced Netlify API Authentication beta version to enable in my settings. I haven't enabled it yet, but I'm not sure if these are the source of my issue.

@earendildev
Copy link

How to check what is gpg key?. My gpg failed to sign the data after git commit -m 'sample text'

I want to push my changes to GitHub in Git command, integrated terminal in VSC, and my profile is Bash, customized in ZSH. I'm deploying to Netlify through continuous deployment from Github. Every time I do "git commit -m "xyz', I get error:

husky > pre-commit (node v14.17.5)
⚠ Some of your tasks use `git add` command. Please remove it from the config since all modifications made by tasks will be automatically added to the git commit index.

ℹ No staged files match any configured task.
error: gpg failed to sign the data
fatal: failed to write commit object

We think it is an authentication issue, related to gpg key. We're able to run gpg2, but not able to sign in with key. We're trying to set the program to gpg2, and we have upgraded to gpg2, but it says it is already installed gpg. How to check what is gpg key? We are not sure where to look/what to do now. Previously I had problems with visibility of my contributions to Gihub, so I have gained Netlify permission to access my repository code. Netlify does this by installing the Netlify GitHub App on my Github account. Everything worked perfect, until I have updated my Macbook Air from Mojave to Big Sur v11. In the same time I had to update my terminal, so I screwed Homebrew upgrade. Yet, my profile is just Bash. Not sure about the last one, sorry! Not least, but last, Netlify just announced Netlify API Authentication beta version to enable in my settings. I haven't enabled it yet, but I'm not sure if these are the source of my issue.

You can run gpg -k to list all your keys

gunpg 2.1 ships with it's own gpg-agent
You just need to install gnupg

Follow LinusU comment above..

brew install gnupg pinentry-mac
echo "pinentry-program /usr/local/bin/pinentry-mac" >> ~/.gnupg/gpg-agent.conf
export GPG_TTY=$(tty)

You can try to gpgconf --kill gpg-agent and gpgconf --kill dirmngr

Also as mentioned by Atejeda try debugging why gpg failed to sign the data
echo "test" | gpg --clearsign

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment