Sometimes, after you have installed GPG for Git and are trying to commit changes, you'll run into the below message:
error: gpg failed to sign the data fatal: failed to write commit object
How to debug the issue
First of all, you should start with a simple 'GIT_TRACE=1' command in order to understand what is happening.
GIT_TRACE=1 git commit
Execute the gpg in the command line again, which should look similar to this:
gpg --status-fd=2 -bsau <your GPG key>
This command will provide you ample context into what happened.
How to implement the solution
Sometimes, the problem is that your GPG key has expired, the solution for which is to run the failing command mannually, as such:
$ echo "dummy" | gpg -bsau <your GPG key> gpg: skipped "<your GPG key>": Unusable secret key gpg: signing failed: Unusable secret key
Othertimes, you might not have properly set the secret key. So, to solve it:
- List the secret keys available in GPG
gpg --list-secret-keys --keyid-format=long
2. Copy and set your key for the user in Git
git config --global user.signingkey <your GPG key>
How to solve the issue on MacOS
Here a few steps in order to try and get the issue fixed:
brew uninstall gpg
brew install gpg2
brew install pinentry-mac
Step 3 applies only if you're lacking Pinetry on your MacOS installation.
gpg --full-generate-key to generate a new key for GPG.
gpg --list-keys to access the key generate previously.
git config --global user.signingkey <your GPG key> to set your key from the above list
git config --global gpg.program /usr/local/bin/gpg
git config --global commit.gpgsign true
As a last step, if you want to export your key to GitHub, run
gpg --armor --export <key>. You can find your GPG keys at this link: https://github.com/settings/keys.