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.

How Jam can help you debug faster

Debugging issues like the "GPG failed to sign data" error can be time-consuming, especially when dealing with cryptographic keys and configuration settings. The good news is you can save yourself a lot of time debugging tickets by asking your team to try logging issues with Jam’s free bug reporting browser extension. Jam makes it easy for anyone to create developer-ready bug reports by auto-including console logs, network requests, and steps to reproduce the issue along with the screen recording.

0:00
/0:09

Replay bugs after they happen. Get all the context you need to fix in a link.

With Jam, you can capture detailed debug data just by recording your screen. Jam packages it all up into a link you can share, or a ticket in your issue tracker. Now everyone can log comprehensive bug reports and developers can fix bugs faster without having to follow up.

Dealing with bugs is 💩, but not with Jam.

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