Skip to content

Instantly share code, notes, and snippets.

@paolocarrasco
Last active November 5, 2024 20:08
Show Gist options
  • Save paolocarrasco/18ca8fe6e63490ae1be23e84a7039374 to your computer and use it in GitHub Desktop.
Save paolocarrasco/18ca8fe6e63490ae1be23e84a7039374 to your computer and use it in GitHub Desktop.
How to understand the `gpg failed to sign the data` problem in git

Problem

You have installed GPG, then tried to commit and suddenly you see this error message after it:

error: gpg failed to sign the data
fatal: failed to write commit object

Debug

For understanding what's going on, first check what git is doing, so add GIT_TRACE=1 at the beginning of the command you used before (git commit or git rebase):

GIT_TRACE=1 git commit

With that you can see what GPG is doing: Probably you will see something like this

10:37:22.346480 run-command.c:637       trace: run_command: gpg --status-fd=2 -bsau <your GPG key>

(Check if your GPG key is correct)

Execute that gpg command again in the command line:

gpg --status-fd=2 -bsau <your GPG key>

👆🏻 With this now you could see what happened in detail!

Solutions

We can have many problems, but I list what I found:

  1. It could be that the GPG key was expired: https://stackoverflow.com/a/47561300/532912

  2. Another thing could be that the secret key was not set properly (In my case the message said gpg: signing failed: No secret key as it can be see in the image below). image It means that is not finding the key that was set. You would need to set up the GPG key in Git (again):

    • List the secret keys available in GPG.
    gpg --list-secret-keys --keyid-format=long
    • Copy your key
    • Set your key for your user in git
    git config --global user.signingkey <your key>
  3. Another popular solution that could help was shared here by @NirajanMahara: https://gist.github.com/paolocarrasco/18ca8fe6e63490ae1be23e84a7039374?permalink_comment_id=3767413#gistcomment-3767413

  4. You can see in the thread of this gist other ways to find the solution to other problems. I recommend to read the Github guide for signing commits with GPG.

Hope it helps!

@rosangelysreyes
Copy link

Thank you! It worked for me following each step ❤️

@lehaiquantb
Copy link

On MacOS, I have to install pinentry-mac to enter passphrase

brew install pinentry-mac
echo "pinentry-program $(which pinentry-mac)" >> ~/.gnupg/gpg-agent.conf
killall gpg-agent

@after-ephemera
Copy link

Thank you for this!

@sudoAlphaX
Copy link

I'm on Windows using the terminal and Gpg4Win (instead of Git Bash), and this helped me solve the gpg: signing failed: No secret key issue.

Make sure that git config gpg.program points to the gpg.exe file from the package by doing the following:

1. Run `where.exe gpg`.

2. If the output returns several executables, locate the one from Gpg4Win (by default, the path is C:\Program FIles (x86)\GnuPG\bin\gpg.exe.

3. Run `git config --global gpg.program <path/to/gpg/from/Gpg4Win>`

(source here)

This worked for me. Thank you very much.

@babud08
Copy link

babud08 commented Oct 25, 2023

I tried this method and still I'm getting this error while commit my changes.

$ git commit -S -m "workflow files commit"
error: cannot spawn gpg2: No such file or directory
error: gpg failed to sign the data
fatal: failed to write commit object

@sudoAlphaX
Copy link

@babud08 i think you have to set git global config for gpg.program

Find your gpg.exe path by using:
where gpg

and use Git Bash to configure the path
git config --global gpg.program <path>

@szympajka
Copy link

Thank you!

@jrgleason
Copy link

gpg --status-fd=2 -bsau F93581548CDBCCB7
[GNUPG:] KEY_CONSIDERED E4C9A7533D31D43B288E162FF93581548CDBCCB7 2
[GNUPG:] BEGIN_SIGNING H10

and it just hangs there.

@shellheim
Copy link

Dude, I just misspelled user.signingkey as user.signkey

@joshjudgex
Copy link

Thank you! My key was wrong. Working now.

@SirPhemmiey
Copy link

What worked for me was setting up program.

  1. first determine the location of your gpg installation. You can use which gpg.
  2. copy the result in the previous step and run the command
git config --global gpg.program "<paste the installation location here>"

@juni37
Copy link

juni37 commented Nov 24, 2023

On MacOS, I have to install pinentry-mac to enter passphrase

brew install pinentry-mac
echo "pinentry-program $(which pinentry-mac)" >> ~/.gnupg/gpg-agent.conf
killall gpg-agent

THANK YOU!!! I had to do this and now it's working for me. HUGE KUDOS TO YOU!!

@DimitarNestorov
Copy link

On my Mac all I had to do was

killall gpg-agent

@bm-synth
Copy link

I you're on WSL2, maybe this can help:

* Add those lines to `~/.gnupg/gpg.conf`

use-agent
pinentry-mode loopback


* Add this line to `~/.gnupg/gpg-agent.conf`

allow-loopback-pinentry

Thanks, worked on WSL2.

Thank you @luiguip , that was the fix for me!

@Muneer94
Copy link

Even after trying the above methods if the problem didn't get resolved, you can restart gpg-agent

systemctl --user status gpg-agent
systemctl --user restart gpg-agent

@naezith
Copy link

naezith commented Dec 5, 2023

On MacOS, I have to install pinentry-mac to enter passphrase

brew install pinentry-mac
echo "pinentry-program $(which pinentry-mac)" >> ~/.gnupg/gpg-agent.conf
killall gpg-agent

This is the true solution 👍

@theBeardA
Copy link

On MacOS, I have to install pinentry-mac to enter passphrase

brew install pinentry-mac
echo "pinentry-program $(which pinentry-mac)" >> ~/.gnupg/gpg-agent.conf
killall gpg-agent

this works like a charm 🚀

@Pablo-pixtm-365
Copy link

Pablo-pixtm-365 commented Dec 13, 2023

I tried differents solutions:

1.- Change the path in "git config --global gpg.program"
2.- See if there were any misspelled variables
3.- echo "test" | gpg --clearsign

But they didn't work.

Finally the solution was run:

git config --global gpg.program gpg

if you use gpg2, change gpg for gpg2

I hope this solution works for you

@scotteg
Copy link

scotteg commented Dec 25, 2023

After upgrading my OSX to Monterey it stoped to work without reason. The GIT_TRACE didn't help to much because everything was correctly set. In the end I reinstalled the GPG Sutie via brew with the command brew reinstall --cask gpg-suite and it fixed the issue.

Doing this resolved my issue. Thanks!

@JorgeNachtigall
Copy link

I you're on WSL2, maybe this can help:

  • Add those lines to ~/.gnupg/gpg.conf
    use-agent 
    pinentry-mode loopback
    
  • Add this line to ~/.gnupg/gpg-agent.conf
    allow-loopback-pinentry
    

GOAT, it worked

@EfrainOlivaresEv
Copy link

🙏
Saved me lots more time after spending a bunch of time debugging ...

@EvieePy
Copy link

EvieePy commented Jan 3, 2024

After spending hours debugging this, the problem was that I was using single quotes for this command, E.g:
git config --global gpg.program 'C:\Program Files (x86)\GnuPG\bin\gpg.exe'

Instead of double quotes, like so:
git config --global gpg.program "C:\Program Files (x86)\GnuPG\bin\gpg.exe"

Thanks for the help though!

@SelinaRaynor
Copy link

Thanks for helping me out :)

@syedamanat
Copy link

syedamanat commented Jan 10, 2024

On MacOS, I have to install pinentry-mac to enter passphrase

brew install pinentry-mac
echo "pinentry-program $(which pinentry-mac)" >> ~/.gnupg/gpg-agent.conf
killall gpg-agent

Thanks for this!

10th-Jan-2024; Still works (mac m1)

@Jphn
Copy link

Jphn commented Jan 11, 2024

Thanks man, it worked for me!

@adriguerra
Copy link

My issue was that I had added the PGP key with my email, while my commits on the repo were signed with my GitHub-provided noreply email address.

To solve this issue, I had to run gpg --edit-key [your key] followed by gpg> adduid to add the second email address.

https://docs.github.com/en/authentication/managing-commit-signature-verification/associating-an-email-with-your-gpg-key

@HimangshuDe
Copy link

HimangshuDe commented Feb 17, 2024

Hello Devs,
Since I am a newcomer to git and GitHub. I have also faced the same problem. Let me state that in detail below.

Problems I have faced:

  1. I have installed git and have generated ssh-key from there and added it to my GitHub account. It worked.

  2. I have installed GPG program for adding GPG key into my account and added to git thereafter. I did all the necessary steps. Added that into my account. Worked fine.

  3. Now whenever I commit any changes, I get that error.

Solution: [Worked in my case]

Since I am on Windows system. I did the following steps that resolved my problem.

  1. Run "Windows Powershell" in admin mode.

  2. Then I set the Execution Policy to AllSigned through this command Set-ExecutionPolicy AllSigned. After that press A and hit Enter.
    Screenshot 2024-02-17 220040

  3. Then I regenerated the GPG key and added that to git and also in my GitHub account.

  4. I went ahead and did the commit, it worked fine!
    Screenshot 2024-02-17 215713

My System Details:

  1. Windows 10 Pro v22H2 [Activated]
  2. Intel i5-3570K CPU Quad Core (Ivy Bridge codename)
  3. 8GB installed DDR3 RAM

Hope you may find this useful!

Regards!
Happy Coding

@usoro007
Copy link

@NirajanMahara
You saved the day and allowed me to get some rest! Thank you!

@danielfromCL
Copy link

Thanks for the help, worked for me! 🙌

@BakiVernes
Copy link

GOAT 🐐

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment