Git のコミットで署名エラーが出た時に対応した内容のメモ

2023-02-07

エラー内容

$ git commit -S -m 'test'

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

Git のログのチェック

GIT_TRACE=true (1, 2 でもOK) を設定するとで詳細なログが出る。

GIT_TRACE controls general traces, which don’t fit into any specific category. This includes the expansion of aliases, and delegation to other sub-programs.
https://git-scm.com/book/en/v2/Git-Internals-Environment-Variables

$ GIT_TRACE=true git commit -m "test" -v
16:04:43.847710 git.c:460               trace: built-in: git commit -m test -v
16:04:43.874360 run-command.c:655       trace: run_command: gpg --status-fd=2 -bsau XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
error: gpg failed to sign the data
fatal: failed to write commit object

デバッグで出てきたコマンドを試してみる

標準入力を渡して動かすっぽかった。

$ echo 'b5fc8dba-39c8-4192-8b33-babfd874f5ee' | gpg --status-fd=2 -bsau XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[GNUPG:] KEY_CONSIDERED XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 2
[GNUPG:] BEGIN_SIGNING H8
gpg: signing failed: No pinentry
[GNUPG:] FAILURE sign 00000000
gpg: signing failed: No pinentry

gpgconf --kill gpg-agent を実行する

gpg: signing failed: No pinentry で検索してみると、以下の Stack Overflow の記事が出てきた。

Very Important

Run gpgconf --kill gpg-agent after change the conf file. Thanks to Jérémie Boulay

https://superuser.com/a/1628810

$ gpgconf --kill gpg-agent

$ GIT_TRACE=true git commit -S -m "test"
16:20:51.081982 git.c:460               trace: built-in: git commit -S -m test
16:20:51.086583 run-command.c:655       trace: run_command: gpg --status-fd=2 -bsau XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
16:20:51.245169 run-command.c:655       trace: run_command: git maintenance run --auto --no-quiet
16:20:51.252719 git.c:460               trace: built-in: git maintenance run --auto --no-quiet
[main 0000000] test
 1 file changed, 1 insertion(+)

無事コミットできた。