Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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. 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!

@justinbalaguer
Copy link

justinbalaguer commented Apr 6, 2022

omg I just need to run export GPG_TTY=$(tty)

  1. then use export GPG_TTY=$(tty)

@atatural
Copy link

atatural commented Apr 20, 2022

lifesaver

dude that was a little bit overrated declaretion, its just my opinion

@pulasthi-Narada
Copy link

pulasthi-Narada commented May 6, 2022

This option is for setting the path in .gitconfig to gpg.exe in the windows os environment.

[gpg]
program = C:\\Program Files (x86)\\GnuPG\\bin\\gpg.exe

@chevonmdphillip
Copy link

chevonmdphillip commented May 13, 2022

@justinbalaguer solution worked for me here.

@Kush1406
Copy link

Kush1406 commented May 15, 2022

@exostin solution worked for me. Thanks

@mnovozhylov
Copy link

mnovozhylov commented May 16, 2022

There's another situation:

sec   dsa3072/AAAAAAAAAAAAA 2010-05-05 [SC] [expires: 2030-05-05]
      BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
uid                 [ultimate] Author Name <author-email@domain.com>

While GitHub documentation operates with AAAAAAAAAAAAA in sections when you need to create and register the key in GPG, git requires BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB, i.e. git config --global user.signingkey BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB, instead of git config --global user.signingkey AAAAAAAAAAAAA

Hopefully, it helps someone.

@ayubov
Copy link

ayubov commented May 26, 2022

I got a case when signing suddenly stopped working. After a long fight nothing has helped except gpgconf --kill gpg-agent

@OliverRC
Copy link

OliverRC commented Jun 7, 2022

If you are on Windows and have used GPG4Win to manage your keys then you need to set the GPG program path.

If you look at where your gpg instance comes from mine looked like

Get-Command gpg | select Source

My gpg path was C:\Program Files (x86)\Gpg4win\..\GnuPG\bin\gpg.exe. That's quite a weird path .

But technically it is the same as "C:\Program Files (x86)\GnuPG\bin\gpg.exe"

So now set GIT to use this path:

git config --global gpg.program "C:\Program Files (x86)\GnuPG\bin\gpg.exe"

Essentially it seemed that the gpg program that was being used was different to the one being run when I used gpg on the command line.

@nguyenvulong
Copy link

nguyenvulong commented Jun 11, 2022

if all of the above did not work for you

I got into a slightly different problem, everything up there ALREADY CHECKED, somehow my gpg signing stopped working - and i don't want to restart my server. After digging around i found this log

Some output of systemctl --user status gpg-agent

6월 12 00:50:55 AISRC gpg-agent[17450]: can't connect to the SCdaemon: IPC connect call failed
6월 12 00:50:55 AISRC gpg-agent[17450]: failed to unprotect the secret key: Operation cancelled
6월 12 00:50:55 AISRC gpg-agent[17450]: failed to read the secret key
6월 12 00:50:55 AISRC gpg-agent[17450]: command 'PKSIGN' failed: Operation cancelled <Pinentry>

Note that until this point, it's not about git anymore, it's about gpg and distro-specific issues.

FIXED (the root cause is pinentryscrewed things up)
Create gpg-agent.conf if you don't have one under your home dir and add the line below (pinentry-program /usr/bin/pinentry-curses)


❯ cat  ~/.gnupg/gpg-agent.conf
pinentry-program /usr/bin/pinentry-curses

Then you have to restart gpg-agent by issuing systemctl --user restart gpg-agent and you may want to log-in/out. Make sure to export GPG_TTY=$(tty) and test again. Good luck

Check my write-up here for the summary nguyenvulong/QA#25

@subhendudash02
Copy link

subhendudash02 commented Jun 17, 2022

The command

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

helped me. I forgot to configure the key after generating.
Thanks!

@MedRedha
Copy link

MedRedha commented Jun 23, 2022

Oh Gosh! You just saved my day 🙏 Thanks a lot @paolocarrasco

@rohitss912
Copy link

rohitss912 commented Jul 11, 2022

@NirajanMahara - Thanks buddy your solution worked for me!

@gmale
Copy link

gmale commented Aug 1, 2022

It could also be due to the fact that you need to enter a password. Run ssh-add before committing.

This was effectively my issue. Somehow running the rebase with GIT_TRACE allowed it to pause and ask for a password (i.e. GIT_TRACE=1 git pull origin master --rebase) and then it all worked. This happened after my computer shut down, which probably explains why the gpg password was required again.

Now that I know GPG was causing this prompt, I followed some of these guides and setup GPG not to prompt for a password any longer.

https://gist.github.com/koshatul/2427643668d4e89c0086f297f9ed2130
https://stackoverflow.com/questions/39780452/prevent-gpg-password-prompt-on-mac
https://unixb0y.de/blog/articles/2019-01/gpg-password-macos-keychain

To test it, I ran the following to verify that it asked me for a prompt. Then, after taking the steps above it stopped asking:

echo test | gpg -e -r myEmail@myemail.com | gpg -d

@victorjatoba
Copy link

victorjatoba commented Sep 15, 2022

Follow the below url to setup signed commit https://help.github.com/en/articles/telling-git-about-your-signing-key

if still getting gpg failed to sign the data fatal: failed to write commit object

this is not issue with git ,this is with GPG follow below steps

  1. gpg --version
  2. echo "test" | gpg --clearsign

if it is showing:

gpg: signing failed: Inappropriate ioctl for device
gpg: [stdin]: clear-sign failed: Inappropriate ioctl for device
  1. then use export GPG_TTY=$(tty)
  2. then try again echo "test" | gpg --clearsign in which PGP signature is.

Output:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

test
-----BEGIN PGP SIGNATURE-----

iLMEAQEKAB0WIQS2V0SFHi18psvDbo7uFF+LP7qc1gUCYLjB2QAKCRDuFF+LP7qc
1r5LBACB1m3Lpl21379qAvVamWcn9isdgdg34t34t43t34t34t434yGQHqikxWL7A5
Ls7giKZYscb30o0rkY6I1W9MjBBW96R2pnaYsioFpsf434dfg54rfdgfdgdfgdfpaIoU3k
JKrYxR7yMjqUv0a2jE+97kh+bSuzqwIkMHyikbABI90lY+4OLw==
=UHKx
-----END PGP SIGNATURE-----
  1. git config -l | grep gpg

Output:

commit.gpgsign=true
gpg.program=gpg
tag.gpgsign=true
  1. apply git commit -S -m "initial commit rocketrocketrocketrocket"
  2. or git config --global commit.gpgsign true

https://stackoverflow.com/questions/39494631/gpg-failed-to-sign-the-data-fatal-failed-to-write-commit-object-git-2-10-0/55993078#55993078

It worked for me. Thanks!

@MaxFoton
Copy link

MaxFoton commented Sep 25, 2022

5. git config -l | grep gpg

it also worked for me! thank you!

@dan-developer
Copy link

dan-developer commented Oct 2, 2022

It seems for every branch I have, I need to execute the export GPG_TTY=$(tty) command before committing.

Is there anyway around this?

It worked for me. Thank you!

@Phrozyn
Copy link

Phrozyn commented Oct 14, 2022

For me the issue is always simply the fact that my vscode terminal window is too small, git needs like half a screen's height and about the width of this comment section for it to surface the GPG prompt, otherwise it errors out and tells you that:

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

so I enlarge the window and voila all fixed when I run git commit again.

@Jeff-Tian
Copy link

Jeff-Tian commented Oct 17, 2022

There's another situation:

sec   dsa3072/AAAAAAAAAAAAA 2010-05-05 [SC] [expires: 2030-05-05]
      BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
uid                 [ultimate] Author Name <author-email@domain.com>

While GitHub documentation operates with AAAAAAAAAAAAA in sections when you need to create and register the key in GPG, git requires BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB, i.e. git config --global user.signingkey BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB, instead of git config --global user.signingkey AAAAAAAAAAAAA

Hopefully, it helps someone.

It helped me, thanks!

@rvmelkonian
Copy link

rvmelkonian commented Oct 18, 2022

worked for me thank you for this!

@nicat97
Copy link

nicat97 commented Oct 19, 2022

Great! Thanks for your help!

@lucymonie
Copy link

lucymonie commented Oct 24, 2022

Thank you 🙏

@lnasc256
Copy link

lnasc256 commented Nov 24, 2022

thank you

@marionorthvolt
Copy link

marionorthvolt commented Dec 1, 2022

I was trying to solve this for 2 days! thanks!

@Lippiece
Copy link

Lippiece commented Dec 2, 2022

  1. then use export GPG_TTY=$(tty)

It also helped to to set it permanently in ~/.profile on Ubuntu (to do so, append export GPG_TTY=$(tty) to the ~/.profile file).

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