[Updated] Fix “Error: Gpg Failed to Sign the Data” Error in Git

Nishil Patel

Nishil Patel

May 9, 2024

5 min read

Share

[Updated] Fix “Error: Gpg Failed to Sign the Data” Error in Git

The “error: gpg failed to sign the data” error can sometimes throw you off guard when you try making GPG-signed commits with Git. This article describes the potential causes of the gpg error and ways to identify and fix it for different operating systems.

Table of Contents

1.

Introduction

2.

What is the GPG Key in Git?

3.

How to Fix the Issue: Based on Operating Systems

4.

FAQs

Introduction

Sometimes, you might get the “error: gpg failed to sign the data” message while creating a code commit using Git with a GPG signature. It indicates a problem with the GPG signing process. Luckily, you can fix the error with just a few commands.

Here’s what the error typically looks like when you try to make a GPG-signed commit that uses an erroneous GPG key:

git commit -m "message"
error: gpg failed to sign the data
fatal: failed to write commit object

What is the GPG Key in Git?

GnuPG or GPG keys are used to authenticate commits and tags while managing code using Git. It’s a cryptographic signature that guarantees that code changes originate from a trusted source. Here’s what the GPG key does:

  • Identity Verification — verifies the author’s identity. For example, if Peter (developer) signs a commit with his GPG key, Carrie (developer) can trust that Peter made the change.
  • Commit Integrity — ensures that the commit remains unchanged after signing. If anyone tampers with the commit, the signature won’t match, alerting others to potential tampering.

How to Fix the Issue: Based on Operating Systems

Here’s how to figure out what’s wrong and fix the “error: gpg failed to sign the data” error:

# Step 1 - Debug the Issue

  • Execute the following command in your git-enabled terminal to get details about Git logs:
GIT_TRACE=1 git commit -m "message"

This sets the “GIT_TRACE” env variable value to 1. Running the command tells Git to print out detailed trace information about the internal process during the commit.

Also Read: GiHub vs. GitLab

After running the command, you usually get a message with the last few lines looking something like:

10:12:21.123456 run-command.c:688     trace: run_command: gpg --status-fd=2 -bsau <your GPG key>
error: gpg failed to sign the data
fatal: failed to write commit object

The message means there’s a problem with your GPG key. The error occurred right after Git tried to run this command: gpg --status-fd=2 -bsau <your GPG key>.

  • Next, run the following command for more details about the issue and get to the root cause of the error.
gpg --status-fd=2 -bsau <your GPG key>

For instance, if you run the command “gpg --status-fd=2 -bsau ABCD1234” with a GPG key that is not set or unavailable, here’s what the output looks like:

gpg: skipped "ABCD1234": No secret key
[GNUPG:] INV_SGNR 9 ABCD1234
[GNUPG:] FAILURE sign 17
gpg: signing failed: No secret key

Facing Software Issues? Create and Share Visual Bug Reports with Console logs, Network Requests, Device Info, and More

Some potential causes for the error include:

  • Expired GPG key
  • Wrong GPG key
  • Improperly configured GPG key
  • Unavailable GPG key

Also Read: chrome://net-internals/#dns: How to Clear DNS Cache on Chrome [Updated]

# Step 2 - Confirm the Cause of the Failing GPG Key

  • To ensure that your GPG key is problematic, diagnose the issue by running the following command:
echo "hello" | gpg -bsau <your GPG key>

Output:

gpg: skipped "<your GPG key": Unusable secret key
gpg: signing failed: Unusable secret key

This command tries to sign the data “hello” with “<your GPG key>,” which is unusable, expired, or may have other issues. Hence, the incorrect response.

# Step 3 - Fix the Issue

Steps 1 and 2 are common for Windows, Linux, and macOS installations. However, the steps to fix the issue differ based on the operating system.

Also Read: Easy Fix for “An Error Occurred in the Upload. Please Try Again Later.”

Here are the steps to fix the issue to follow based on your OS:

For Windows OS/Linux OS

Here’s how to fix the issue if you have an expired or wrong GPG key for your Git commit:

1. Get the list of available keys to choose from with the following command:

gpg --list-secret-keys --keyid-format=long

2. Copy a valid key from the list, paste it into the following command, and run it:

git config --global user.signingkey <your GPG key>

3. Run the following to enable Git to sign all commits by default:

git config --global commit.gpgsign true

The above steps should resolve the GPG error. If the GPG is unavailable or not set properly, you can create a new GPG key, set the key, and then try to proceed with your commit process.

For macOS

Here’s how to fix the GPG issue on Mac machines:

1. Uninstall gpg that’s pre-built into macOS:

brew uninstall gpg

2. Install gpg2 with Homebrew:

brew install gpg2

3. Git might not automatically use the gpg2 package. Set the global path for gpg2 using:

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

4. Restart the terminal for the changes to take effect.

5. To list available keys, run the command:

gpg --list-secret-keys --keyid-format=long

6. If needed, you can also generate a new GPG key using:

gpg --full-generate-key

7. Set the global signing key with the correct key using:

git config --global user.signingkey <your GPG key> 

8. Run the following command to configure Git to sign all commits by default:

git config --global commit.gpgsign true

By following these steps and considering the macOS specifics, you should be able to fix the GPG error.

Accelerate Your Debugging Sessions with Comprehensive Bug Reports

FAQs

Use gpg --armor --export <your GPG key> and add the exported key to GitHub’s GPG keys settings.

Written by

Nishil Patel | CEO & Founder

Follow

Nishil is a successful serial entrepreneur. He has more than a decade of experience in the software industry. He advocates for a culture of excellence in every software product.

Subscribe to our updates

Meet the Author: Nishil Patel, CEO, and Co-founder of BetterBugs. With a passion for innovation and a mission to improve software quality.

We never spam.

Nothing here!
Show us some love 💖. Try BetterBugs today if you haven’t already. A quick feedback and a rating on our Chrome web store page would be awesome!

Share your experience with the founderhere!

Don’t wait! Start reporting now.