Emacs, git, gpg and 'failed to sign the data'

April 3, 2018

I'm not sure is this (un)common case, but when I used git with signing commits from Emacs with VC or Magit, I was getting this:

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

Curiously, many had similar problem, but not from Emacs which is my case. On other hand, git signing from terminal worked without any problems for me.

Further digging, mostly by trying to run manually git command with various options from minibuffer (running git in eshell, shell or ansi-shell didn't reproduce above error), I managed to get this behavior. Interestingly, gpg will always use TTY (terminal), even for output.

Solution is to use –no-tty with gpg/gpg2 commands. Sadly, git doesn't allow command line options in gpg configuration section, so this will not work:

[gpg]
   program = gpg2 --no-tty

However, adding the following line in gpg configuration ($HOME/.gnupg/gpg.conf) will solve the problem:

# disable tty output by default
no-tty

In case you are running gpg-agent, reload it with:

$ gpg-connect-agent reloadagent /bye