Skip to content

Instantly share code, notes, and snippets.

@bynect
Last active June 3, 2021 12:04
Show Gist options
  • Save bynect/86eb5a6649e6c1d7b6890099230226cb to your computer and use it in GitHub Desktop.
Save bynect/86eb5a6649e6c1d7b6890099230226cb to your computer and use it in GitHub Desktop.
Troubleshooting for `gpg failed to sign the data` error.

Error

You have linked GPG to git and Github, following the instructions on the documentation, but when you tried to sign a commit this message shows up:

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

Try

You can track what GPG and git are trying to do with GIT_TRACE=1:

$ GIT_TRACE=1 git commit -S -m "Trying to commit something..."
#-S is not needed if you have set `git config commit.gpgsign` to `true`

If the result looks similar to this:

11:32:49.709208 git.c:328               trace: built-in: git 'commit' '-S' '-m' 'Trying to commit something...'
11:32:49.712102 run-command.c:626       trace: run_command: 'gpg' '--status-fd=2' '-bsau' '<your-gpg-key>'
error: gpg failed to sign the data
fatal: failed to write commit object

You can try to manually rerun the command that caused the error:

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

This should give you a more precise error.

Solutions

If you are tying to use GPG-signed commits from a WSL shell and you have correctly followed all the steps in the documentation one possible solution for your problem is to add to your file .bashrc (or .zshrc if you are using zsh like me) this line:

export GPG_TTY=$(tty)

It will enable a passphrase prompt for GPG. After that you will be prompted to insert your key passphrase from GPG, and then the problem should be resolved. This is the same problem that occured to me, basically the keys and the configuration is working well but GPG didn't asked you the passphrase.

Note: Even if you are not using WSL you can to try this solution, especially if you are getting an Inappropriate ioctl for device error or GPG doesn't ask you the passphrase.


I also found online this solutions for Mac OS X users:

brew upgrade gnupg
brew link --overwrite gnupg
brew install pinentry-mac
echo "pinentry-program /usr/local/bin/pinentry-mac" >> ~/.gnupg/gpg-agent.conf
killall gpg-agent

This should install gpg2 and configure it.


Other possible causes of the error may be:

  • The key is expired, like in this case.
  • The secret key wasn't set.
  • Something had been appended to the key, or other typing error.
  • The key was generated in the wrong format.
  • Some linux distos use gpg2 or gpg1 instead of gpg.

If none of these solutions worked you can give a look at this official GPG troubleshooting page.

Hope that helps.

@NirajanMahara
Copy link

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 🚀🚀🚀🚀"
  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

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