'Git: gpg failed to sign the data' in visual studio code

Maybe git cannot find gpg? That was my problem with working with VSCode and using Remote-Containers to create development containers. Try running this in the Terminal within VSCode (in the container)

git config --global --unset gpg.program
git config --global --add gpg.program /usr/bin/gpg

or wherever your gpg is located. You can find out by typing

which gpg

If that works then you can put it in your Dockerfile for your development container.


first make sure to add export GPG_TTY=$(tty) in your .bashrc

Apparently VSCode doesn't ask for the passphrase and that's why it gives an error. I don't know the reason. My personal solution do a console commit first or run the following line

echo "test" | gpg --clearsign

I had the same issue a few days ago while using VS Code with WSL. The problem is that VS Code doesn't load the .profile file (and all the environment variables in it) correctly (try to run this command but it doesn't get the correct result: echo $GPG_TTY). Fortunately, setting the "-l" option for shell args in VS Code preferences worked for me. This ensures that the .profile (or .zprofile) file is successfully loaded. I added these lines to settings.json:

"terminal.integrated.shellArgs.linux": [
    "-l"
]

Make sure to add export GPG_TTY=$(tty) in your .profile file and restart your terminal and VS Code.