Nishil Patel
May 9, 2024
5 min read
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.
1.
Introduction
2.
What is the GPG Key in Git?
3.
How to Fix the Issue: Based on Operating Systems
4.
FAQs
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
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:
Here’s how to figure out what’s wrong and fix the “error: gpg failed to sign the data” error:
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>.
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
Some potential causes for the error include:
Also Read: chrome://net-internals/#dns: How to Clear DNS Cache on Chrome [Updated]
echo "hello" | gpg -bsau <your GPG key>
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.
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.
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.
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.
Share your experience with the founderhere!