简介

使用 GPG 给 Commit 签名,网上已经有很多教程,但是在 Emacs 使用 Magit 作为 Git 客户端时, 还是碰到了几个问题。

Emacs 唤起 GPG Passphrase 界面异常

默认 GPG Passphrase 界面使用的是 pinentry-curses ,GUI Emacs 兼容性不好,需要使用 pinentry-mac , 会弹出 Mac UI 的密码框:

  1. 安装 pinentry-mac

    brew install pinentry-mac
    
  2. 修改 ~/.gnupg/gpg-agent.conf 配置默认使用 pinentry-mac ,加上这行:

    pinentry-program /usr/local/bin/pinentry-mac

无论命令行,还是 Emacs 都报错

报错内容:

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

解决方案,重启 gpg-agent:

killall gpg-agent && gpg-agent --daemon --pinentry-program /usr/local/bin/pinentry-mac

Magit 默认签名

Magit 现在使用 Transient 作为 UI,默认关闭了签名的配置项,需要手动打开:

  1. c 打开 commit 界面
  2. C-x l 打开 Transient Suffixes 配置界面
  3. -S 选中 gpg-sign
  4. 1 输入等级,小于 4 会展示

Magit 配置页面可以通过 C-x C-s 保存当前配置,所以进入到 Commit 界面后

  1. -S 使用签名
  2. -s Commit 信息添加签名行
  3. C-x C-s 保存配置