20

I have recently tried the Windows Subsystem for Linux lately and as I was attempting to sign my git commits with a recently generated GPG key it spewed out,

verticalfile30@DESKTOP-U284V9I:~/cpo/wsltest$ git commit -S -m "gpg signing"
error: gpg failed to sign the data
fatal: failed to write commit object

I have used git long enough to know that I have to set the local or global variables on git to use my GPG key.

After the first error, I tried to generate another key but that also did not work, including the subkeys.

My GPGs were encrypted in RSA and RSA (default).

I have tried using articles on help.github.com but to no avail.

(Here are some specific sources.) https://help.github.com/en/articles/telling-git-about-your-signing-key

https://help.github.com/en/articles/signing-commits

Here is most of the terminal log

verticalfile30@DESKTOP-U284V9I:~/cpo/wsltest$ git config --global commit.gpgsign true
verticalfile30@DESKTOP-U284V9I:~/cpo/wsltest$ gpg2 --list-secret-keys --keyid-format LONG
gpg: checking the trustdb
gpg: marginals needed: 3  completes needed: 1  trust model: pgp
gpg: depth: 0  valid:   2  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 2u
gpg: next trustdb check due at 2021-08-22
/home/verticalfile30/.gnupg/pubring.kbx
---------------------------------------
sec   rsa4096/498F47808959B459 2019-08-23 [SC] [expires: 2021-08-22]
      A533C851D2905FC63C161831498F47808959B459
uid                 [ultimate] Vert Simon (Key#3) <stopmotion45c@gmail.com>
ssb   rsa4096/E4E65BE559FFBE2C 2019-08-23 [E] [expires: 2021-08-22]

sec   rsa4096/B3C88EE54DC15CC9 2019-08-23 [SC]
      87F5399E6BFEF88C1C64794CB3C88EE54DC15CC9
uid                 [ultimate] Vert S (Sop) <stopmotion45c@gmail.com>
ssb   rsa4096/E868623210106F9D 2019-08-23 [E]

verticalfile30@DESKTOP-U284V9I:~/cpo/wsltest$ git config --global user.signingkey E868623210106F9D
verticalfile30@DESKTOP-U284V9I:~/cpo/wsltest$ git commit -S -m "gpg signing"
error: gpg failed to sign the data
fatal: failed to write commit object
verticalfile30@DESKTOP-U284V9I:~/cpo/wsltest$ git config --global user.signingkey B3C88EE54DC15CC9
verticalfile30@DESKTOP-U284V9I:~/cpo/wsltest$ git commit -S -m "gpg signing"
error: gpg failed to sign the data
fatal: failed to write commit object

Many sources online, as well as other questions on Stack Overflow, ended up saying the same thing, set the global variables. Is there something I am forgetting or getting wrong? Thanks.

Verticalfile30
  • 303
  • 1
  • 2
  • 7

3 Answers3

35

That was followed in microsoft/WSL issue 4029

But in that case, it was:

My key has a passphrase but there is no prompt to enter the passphrase.

But then I read this:

Step 3 is export GPG_TTY=$(tty), which sends the prompt to tty.
After entering my passphrase, everything works.

Example: danhorst/dotfiles commit 805a779, which follows gpg failed to sign the data fatal: failed to write commit object [Git 2.10.0].


You can also refer to "How to sign your commits to GitHub using Visual Studio Code on Windows 10 and WSL2" from Christopher Hamilton.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 2
    Can confirm this solution worked in windows 11 – Abir Taheer Jul 04 '21 at 00:30
  • @AbirTaheer Windows 11?! Now I have to check if I can run this: https://github.com/rcmaehl/WhyNotWin11 – VonC Jul 04 '21 at 09:26
  • Can confirm this works on Windows 10 & WSL after searching the internet for several hours, making sure the variables were set, etc. The tip off should be... if you have done everything else but are NOT prompted for your GPG passphrase, this could be your working answer. – BillieM Feb 11 '23 at 19:18
  • @BillieM I have tested it with Windows 11 since my last comment, and I no longer have access to a Windows 10. – VonC Feb 11 '23 at 21:38
11

If you are using Kleopatra to manage your GPG keys in Windows, you can add the following to your ~/.gitconfig within your WSL environment (update your path accordingly):

[gpg]
    program = /mnt/c/Program Files (x86)/GnuPG/bin/gpg.exe

After adding this, the prompt to enter my password for my GPG key appeared as usual.

Background: Using Docker had been extremely slow for me when using mounted volumes. I decided to just deploy the repository within an Ubuntu WSL environment directory (i.e. ~/projects/my_project) and up the containers from there using Docker WSL2 support. I use Visual Studio Code so I could open the editor into the WSL environment directly and work from there. However, I lost the ability to sign my commits. Thus, the solution above allowed me to retain my existing workflow.

Tan
  • 196
  • 3
  • 6
  • I have been trying to get this working for a while. I generated my key in Kleopatra, and I was trying to sign commits in WSL2. It was using the linux gpg executable and wasn't working. This allowed me to commit on the WSL2 command line! Thanks! – Steve Storck Nov 12 '21 at 22:47
  • The solution worked for me and was the simplest, thank you. – mcmikecreations May 22 '23 at 19:23
  • gold for those who switch from a working win11 environment to wsl2. Thank you – f-re Jul 12 '23 at 15:43
9

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

gpg failed to sign the data fatal: failed to write commit object [Git 2.10.0]

Nirajan Mahara
  • 1,644
  • 9
  • 7
  • Although this solve the problem, When i close my terminal and open again, this issues come back – Shadab Faiz Sep 23 '21 at 05:02
  • After step 2. I got the error `gpg: can't connect to the agent: IPC connect call failed` - which meant that WSL wasn't running the `gpg-agent` - [this answer worked for me](https://stackoverflow.com/a/47259115/5662) – Andrew Jan 12 '22 at 11:31