Error: gpg failed to sign the data

If you’re facing issues signing with gpg, here’s what you can do:

$ git commit -m "initial commit"
error: gpg failed to sign the data
fatal: failed to write commit object

Double-check that gpg is working:

$ echo "test" | gpg --clearsign
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

test
gpg: signing failed: No pinentry
gpg: [stdin]: clear-sign failed: No pinentry

Note: It’s not working; Looks like it cannot find pinentry.

Quick Fix

Find out the location of pinentry:

# Find out pinentry location
$ which pinentry
/run/current-system/profile/bin/pinentry

Stop the running gpg daemon:

# kill running daemon
pkill gpg-agent

Start daemon:

# run new daemon
gpg-agent --pinentry-program=/run/current-system/profile/bin/pinentry --daemon