GPG failed to sign the data fatal: failed to write commit object - How to solve the error in Git

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:

  1. 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:

  1. Run brew uninstall gpg
  2. Run brew install gpg2
  3. Run brew install pinentry-mac

Step 3 applies only if you're lacking Pinetry on your MacOS installation.

4. Use gpg --full-generate-key to generate a new key for GPG.

5. Use gpg --list-keys to access the key generate previously.

6. Use git config --global user.signingkey <your GPG key> to set your key from the above list

7. Run git config --global gpg.program /usr/local/bin/gpg

8. Run 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.

Dealing with bugs is 💩, but not with Jam.

Capture bugs fast, in a format that thousands of developers love.
Get Jam for free