我参考了几篇关于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_TRACE=1显示了git实际在做什么:

$ GIT_TRACE=1 git commit -m "example commit message"
20:52:58.902766 git.c:328               trace: built-in: git 'commit' '-vvv' '-m' 'example commit message'
20:52:58.918467 run-command.c:626       trace: run_command: 'gpg' '--status-fd=2' '-bsau' '23810377252EF4C2'
error: gpg failed to sign the data
fatal: failed to write commit object

现在手动运行失败命令:

$ echo "dummy" | gpg -bsau 23810377252EF4C2
gpg: skipped "23810377252EF4C2": Unusable secret key
gpg: signing failed: Unusable secret key

原来我的钥匙过期了,这不是我的错。

其他回答

我也见过类似的答案,但没有一个完全适合我。在Linux上,我必须杀死并重新启动我的gpg-agent:

$ pkill gpg-agent
$ gpg-agent --daemon
$ git commit ...

这招对我很管用。看起来你确实需要用户。Signingkey设置为您的私钥,以及从其他一些评论说。

$ git config --global user.signingkey [your_key_hash]

有点奇怪,但要确保你的终端足够大!你可以通过运行echo test | gpg—clearsign来判断它是否太小,它会给你一个非常明显的错误消息,让你知道。如果它不够大,您的GPG代理就不能显示它的小ncurses框。

如果您使用GUI代理或不使用ncurses的东西,这一点将不适用。

如果与GPG密钥的uid相关联的电子邮件与您在git中使用的电子邮件不同,则需要向密钥添加另一个用户id,或者使用与电子邮件完全匹配的密钥。

您可以通过以下方式添加另一个UID:

$ GPG—编辑键

见莫https://superuser.com/questions/293184/one-gnupg-pgp-key-pair-two-emails

我在Ubuntu上突然开始出现这种情况,不确定是不是最近的一些更新做到了,但没有一个现有的问题适用于我(我设置了GPG_TTY,尝试杀死代理等)。独立的gpg命令失败,错误如下:

$ echo "test" | gpg --clearsign
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

test
gpg: signing failed: Operation cancelled
gpg: [stdin]: clear-sign failed: Operation cancelled

我试着用——debug-all选项运行gpg,注意到下面的输出:

gpg: DBG: chan_3 <- INQUIRE PINENTRY_LAUNCHED 27472 gnome3 1.1.0 /dev/pts/6 screen-256color -
gpg: DBG: chan_3 -> END
gpg: DBG: chan_3 <- ERR 83886179 Operation cancelled <Pinentry>
gpg: signing failed: Operation cancelled

以上说明pinentry程序存在一些问题。Gpg通常会为我运行pinentry-curses,所以我把它改成了pinentry-tty(我必须先安装它),错误就消失了(尽管我不再得到全屏密码输入,但我不喜欢这样)。为了做出这个改变,我必须在~/.gnupg/gpg-agent.conf中添加一行pinentry-program /usr/bin/pinentry-tty,并使用gpgconf——kill gpg-agent来杀死代理(它会在下次重新启动)。

如果gnupg2和gpg-agent 2。如果使用了x,请务必设置环境变量GPG_TTY。

export GPG_TTY=$(tty)

有关常见问题,请参阅GPG的文档。