我参考了几篇关于Git 2.10发行说明中的漂亮属性的文章。将git升级到2.10.0,并对全局的.gitconfig进行了更改,结果如下-

[filter "lfs"]
    clean = git-lfs clean %f
    smudge = git-lfs smudge %f
    required = true
[user]
    name = xyz
    email = abc.def@gmail.com
    signingkey = AAAAAAA
[core]
    excludesfile = /Users/xyz/.gitignore_global
    editor = 'subl' --wait
[difftool "sourcetree"]
    cmd = opendiff \"$LOCAL\" \"$REMOTE\"
    path = 
[mergetool "sourcetree"]
    cmd = /Applications/SourceTree.app/Contents/Resources/opendiff-w.sh \"$LOCAL\" \"$REMOTE\" -ancestor \"$BASE\" -merge \"$MERGED\"
    trustExitCode = true
[alias]
    lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
[color "diff"]
    old = red strike
    new = green italic

但是现在我尝试使用

git commit -a -S -m "message"

我看到下面的错误-

你需要一个密码来解锁秘钥 用户:“XYZ(数字签名)” 2048-bit RSA key, ID AAAAAAAA, created 2016-07-01 错误:GPG数据签名失败致命:写提交失败 对象

注意-我仍然可以使用git commit -a -m "message"来提交更改

有办法克服同样的问题吗?或者在gpg配置中需要做任何改变以适应git的升级?


更新1

还寻求进一步的有用性,以下是是否有一种方法在Git中使用GPG密钥“自动签名”提交?我已经配置了密钥使用

git config --global user.signingkey ED5CDE14(with my key) 
git config --global commit.gpgsign true

很明显得到了相同的错误。


当前回答

我发现检查git commit在底层做什么非常有用。使用GIT_TRACE=1运行以下提交:

GIT_TRACE=1 git commit -S -m "MESSAGE"

这将显示git在提交时使用的用户名、电子邮件和签名密钥。

在我的例子中,我发现git为签名提交选择了错误的用户和关键细节。我主要打算使用repo的本地配置,而不是全局配置,并将以下内容添加到本地git配置(位于“REPO_PATH/.git/config”),让提交在终端和VSCode中都可以工作

[user]
    name = USER NAME
    email = USER EMAIL
    signingKey = SIGNING KEY

也可以设置如下:

git config --local user.name "USER NAME"
git config --local user.email "USER EMAIL"
git config --local user.signingkey "USIGNING KEY"

其他回答

2016年10月更新:第871期提到“Git 2.9.3中签名停止工作”

Git for Windows 2.10.1于两天前(2016年10月4日)发布,修复了提交和标记的交互式GPG签名。

git中最近的gpg-sign更改(在Linux上没有问题)暴露了一个问题,即在Windows上,非msys2 -git与MSYS2-gpg交互的方式。


最初的回答:

阅读“7.4 Git工具-签名你的工作”,我假设你有你的“用户”。Signingkey”配置集。

最后一次围绕gpg的大重构(在Git 2.10之前)是在提交2f47eae2a,在这里错误消息被移动到gpg-interface.c

该文件的日志显示了提交af2b21e (Git 2.10)中最近的更改

gpg2 already uses the long format by default, but most distributions seem to still have "gpg" be the older 1.x version due to compatibility reasons. And older versions of gpg only show the 32-bit short ID, which is quite insecure. This doesn't actually matter for the verification itself: if the verification passes, the pgp signature is good. But if you don't actually have the key yet, and want to fetch it, or you want to check exactly which key was used for verification and want to check it, we should specify the key with more precision.

因此,请检查您如何指定用户。签名密钥配置和正在使用的GPG版本(gpg1或gpg2),以查看它们是否对错误消息有任何影响。

还有提交0581b54,它改变了gpg未能签署数据错误消息的条件(作为提交0d2b664的补充):

我们目前根本不从stderr读取。然而,在未来的补丁中,我们会想要这样做,所以这也为我们做好了准备(在这种情况下,gpg确实会在读取所有输入之前写入,尽管同样,键uid不太可能填满管道缓冲区)。

提交4322353显示gpg现在使用临时文件,因此可能存在正确的问题。

让我们转换为使用tempfile对象,该对象处理 对我们来说困难的案子,再加上失踪的清理电话。

我也遇到了同样的问题。我很高兴地报告,问题不在于git 2.10.0,而在于gnupg 1.4.21。

暂时将gnupg降级到1.4.20为我解决了这个问题。

如果你正在使用自制程序,并且像我一样升级了你的包,你可能只需要运行brew开关gnupg 1.4.20来恢复。

git的痕迹非常暴露了我的情况……

   GIT_TRACE=1 git commit -m "a commit message"
   13:45:39.940081 git.c:344               trace: built-in: git commit -m 'a commit message'
   13:45:39.977999 run-command.c:640       trace: run_command: gpg --status-fd=2 -bsau 'full name <your-email@domain.com>'
   error: gpg failed to sign the data
   fatal: failed to write commit object

我需要根据git检查的格式生成一个初始密钥。最好在日志中复制上面传递给-bsau的值,然后在下面使用。

所以它变成了,

   gpg --quick-generate-key "full name <your-email@domain.com>"

然后就成功了。

以上都不适合我,我通常使用我的IDE终端。

我时不时地得到这个错误,在大多数情况下,它工作得很好。 我在跑步后发现了这个问题

 echo "test" | gpg --clearsign

gpg: signing failed: Screen or window too small
gpg: [stdin]: clear-sign failed: Screen or window too small

解决方案:增加终端窗口大小。

我在OSX上遇到了这个问题。

最初的回答:

这似乎是一个gpg更新(brew)改变了gpg的位置到gpg1,你可以改变二进制的git查找gpg:

git config --global gpg.program gpg1

如果没有gpg1: brew,请安装gpg1。

答:更新

看起来gpg1被弃用了/“慢慢地退出使用”,所以你可能真的应该更新到gpg2,不幸的是这涉及到相当多的步骤/一点时间:

brew upgrade gnupg  # This has a make step which takes a while
brew link --overwrite gnupg
brew install pinentry-mac

在老的自制啤酒上:

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

在M1 mac等较新的系统上:

echo "pinentry-program /opt/homebrew/bin/pinentry-mac" >> ~/.gnupg/gpg-agent.conf 
killall gpg-agent

第一部分安装gpg2,后一部分是使用它所需的hack。关于故障排除,请参阅这个答案(虽然这是关于linux而不是brew),它建议一个很好的测试:

echo "test" | gpg --clearsign  # on linux it's gpg2 but brew stays as gpg

如果此测试成功(没有包含PGP签名的错误/输出),则您已经成功更新到最新的gpg版本。

现在您应该可以再次使用git签名了! 值得注意的是,你需要:

git config --global gpg.program gpg  # perhaps you had this already? On linux maybe gpg2
git config --global commit.gpgsign true  # if you want to sign every commit

注意:在你运行了一个有签名的提交之后,你可以验证它的签名:

git log --show-signature -1

其中将包括最后一次提交的GPG信息。