git - gpg onto mac osx: error: gpg failed to sign the data

git - gpg onto mac osx: error: gpg failed to sign the data

我从 brew 安装了 GPG。

brew install gpg

是gnupg2-2.0.30_2.

当我提交时,我确实收到一条错误消息:

You need a passphrase to unlock the secret key for
user: "Max Mustermann (mycomment) <mm@test.de>"
2048-bit RSA key, ID 1111AAAA, created 2017-01-05 

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

我使用了命令:

gpg --list-secret-keys | grep ^sec

它给了我回报:

sec   2048R/1111AAAA 2017-01-05

然后我使用了这个命令:

git config --global user.signingkey 1111AAAA

commit 返回相同的错误消息。

我该如何解决这个问题?

如果您根本没有收到输入密码的提示,解决方案可能只是安装一个程序来帮助输入密码。最常见的是 pinentry.

brew install pinentry-mac

所以安装它并重试 可能 让事情正常进行。但如果不是,另一件事就是确保 git 它 using/finding 正确的 GPG 程序。这些天你真的应该使用 gpg2,所以如果你还没有安装它,请执行以下操作:

gpg --version

…并确保它表明您安装了 GnuPG 版本 2+(而非版本 1)。

如果您已经安装了 GnuPG 2+ 和 pinentry,请试试这个:

echo "pinentry-program /usr/local/bin/pinentry-mac" >> ~/.gnupg/gpg-agent.conf

…或者,更稳健:

echo "pinentry-program $(which pinentry-mac)" >> ~/.gnupg/gpg-agent.conf

...然后重试。

您可能还需要停止 gpg-agent:

gpgconf --kill gpg-agent

您不需要手动重启它——它会在需要时自动重启。

注意: 一些评论者提到在进行更改后需要重新启动 — 但似乎唯一的影响可能是导致 gpg-agent 重新启动。所以像上面描述的那样手动杀死gpg-agent应该就足够了。

任何在 MacOS 机器上遇到这个问题的人,试试这个:

  1. brew uninstall gpg
  2. brew install gpg2
  3. brew install pinentry-mac(如果需要)
  4. gpg --full-generate-key 使用算法创建密钥。
  5. 通过执行得到生成的密钥:gpg --list-keys
  6. 在此处设置密钥git config --global user.signingkey <Key from your list>
  7. git config --global gpg.program /usr/local/bin/gpg
  8. git config --global commit.gpgsign true
  9. 如果您想将密钥导出到 GitHub,则:gpg --armor --export <key> 并将此密钥添加到 GitHub 的 GPG 密钥:https://github.com/settings/keys(包括开始和结束行)

如果问题仍然存在:

test -r ~/.bash_profile && echo 'export GPG_TTY=$(tty)' >> ~/.bash_profile

echo 'export GPG_TTY=$(tty)' >> ~/.profile

如果问题仍然存在:

安装 https://gpgtools.org 并通过按菜单栏中的 Sign 对您使用的密钥进行签名:Key->Sign

如果问题仍然存在:

转到: 您的全局 .gitconfig 文件,在我的例子中位于: /Users/gent/.gitconfig 并修改 .gitconfig 文件 (请确保 Email 和 Name 与您在生成 Key 时创建的相同):

[user]
 email = gent@youremail.com
 name = Gent
 signingkey = <YOURKEY>
[gpg]
 program = /usr/local/bin/gpg
[commit]
 gpsign = true
 gpgsign = true
[filter "lfs"]
 process = git-lfs filter-process
 required = true
 clean = git-lfs clean -- %f
 smudge = git-lfs smudge -- %f
[credential]
 helper = osxkeychain

p.s 我从我之前的回答中得到了这个答案:

我收到相同的错误消息,发现我的密钥已过期。 因此,使用以下方法检查密钥过期可能是个好主意:

gpg --list-keys

如果您的密钥也已过期,您可以通过以下方式调整到期日期:

gpg --edit-key <YOUR_KEY>

然后:

gpg> expire
...enter the new expiration date...
gpg> save

改为安装 GPGSuite,它具有用于生成密钥的 GUI。

您可以看到更多详情here

如果您在 macOS 中仍然遇到问题, 打开 ~/.gitconfig 并将 [gpg] 以下的任何内容更改为 program = /usr/local/bin/gpg

在我的例子中,user.signingkey 设置错误....复制正确的签名密钥问题解决了

我 运行 brew upgrade 在 macOS 上,它破坏了我的 gpg 登录 git,大概是在更新 gpg 包时。我假设有一种更简单的方法可以强制 gpg 服务正确重启(我假设升级包时主动 运行 损坏了),但我对此的修复非常简单:只需重新启动计算机即可。