git commit 签名失败:密钥不可用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36810467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:06:41  来源:igfitidea点击:

git commit signing failed: secret key not available

git

提问by Emilio Menéndez

I am getting this error when trying to commit using Git.

尝试使用 Git 提交时出现此错误。

gpg: skipped "name <[email protected]>": secret key not available
gpg: signing failed: secret key not available
error: gpg failed to sign the data
fatal: failed to write commit object

I have generated a new key as below but it still gives the same error

我生成了一个如下的新密钥,但它仍然给出相同的错误

gpg --list-keys
~/.gnupg/pubring.gpg
--------------------------------
pub   2048R/35F5FFB2 2016-04-23
uid                  name (New key) <[email protected]>
sub   2048R/112A8C2D 2016-04-23

The secret key is the same as above

秘钥和上面一样

I have found this Generating a GPG key for git taggingand followed the steps but it still doesn't work, any idea?

我发现这个 为 git 标记生成 GPG 密钥并按照步骤操作,但它仍然不起作用,知道吗?

采纳答案by Leonardo Emanuel Alifraco

You need to configure the secret key before using it.

您需要在使用之前配置密钥。

git config user.signingkey 35F5FFB2

Or declare it globally if you want to use the same key for every repository.

如果您想为每个存储库使用相同的密钥,或者全局声明它。

git config --global user.signingkey 35F5FFB2

Source: Git Tools - Signing Your Work

资料来源:Git 工具 - 签署你的工作

回答by Wakeel

This worked for me on Windows 10:

这在 Windows 10 上对我有用:

git config --global gpg.program "C:\Program Files (x86)\GnuPG\bin\gpg.exe"

This was the error I got prior to the fix:

这是我在修复之前遇到的错误:

gpg: skipped "3E81C*******": secret key not available
gpg: signing failed: secret key not available
error: gpg failed to sign the data
fatal: failed to write commit object

回答by paul van bladel

What worked for me was adding

对我有用的是添加

git config --global gpg.program "C:/Program Files (x86)/GNU/GnuPG/gpg2.exe"

If you want to find the full path of gpg2.exe:

如果要查找 gpg2.exe 的完整路径:

where gpg2.exe

回答by alphayax

I'like to complete all these answers, cause I've got many issues with this.

我想完成所有这些答案,因为我对此有很多问题。

These exemples use the --globalflag, but you can remove it if you want to to these things locally.

这些示例使用该--global标志,但如果您想在本地处理这些事情,您可以将其删除。

Configure secret key in git

在 git 中配置密钥

git config --global user.signingkey 35F5FFB2

Configure witch gpg program tu use in git (optional)

配置在 git 中使用的女巫 gpg 程序(可选)

Some systems (Ubuntu for exemple) can have gpgand gpg2at the same time. You need to specify you'll use gpg2

某些系统(例如 Ubuntu)可以同时具有gpggpg2。您需要指定您将使用gpg2

git config --global gpg.program gpg2

Export GPG_TTY (optional)

导出 GPG_TTY(可选)

It is possible if you use these command in an ssh environment that you have the following error : Inappropriate ioctl for deviceor gpg: échec de la signature?: Ioctl() inapproprié pour un périphérique. This can be fixed via :

如果您在 ssh 环境中使用这些命令,则可能会出现以下错误:Inappropriate ioctl for devicegpg: échec de la signature?: Ioctl() inapproprié pour un périphérique. 这可以通过以下方式修复:

export GPG_TTY=$(tty)

Auto enable GPG singing (optional)

自动启用 GPG 唱歌(可选)

git config --global commit.gpgsign true

回答by CodeWizard

You have to set the variable GNUPGHOME. Without it, GnuPG is not able to find your keys.

您必须设置变量GNUPGHOME。没有它,GnuPG 无法找到您的密钥。

# On unix add it to your path

# On windows it will usually be under: 
<drive>:\Users\<username>\AppData\Roaming\gnupg

On Unix it simply adding it to the path.
On Windows you have to open the control panel and set it as

在 Unix 上,它只是将它添加到路径中。
在 Windows 上,您必须打开控制面板并将其设置为

System Variable
  Name: GNUPGHOME
  Path: <drive>:\Users\<username>\AppData\Roaming\gnupg

回答by will824

I had a situation in which the same was happening to me in a Windows 10 machine.

我遇到过在 Windows 10 机器上发生同样的情况。

$ git commit -m "Improve logging, imports and show time executed"
gpg: signing failed: Operation cancelled
gpg: signing failed: Operation cancelled
error: gpg failed to sign the data
fatal: failed to write commit object

The commands "C:\Program Files (x86)\GnuPG\bin\gpg.exe" --list-secret-keys --keyid-format LONGand gpg --list-secret-keys --keyid-format LONGwhere giving me complete different results!

命令"C:\Program Files (x86)\GnuPG\bin\gpg.exe" --list-secret-keys --keyid-format LONGgpg --list-secret-keys --keyid-format LONG哪里给了我完全不同的结果!

$ where gpg
C:\Program Files\Git\usr\bin\gpg.exe
C:\Program Files (x86)\GnuPG\bin\gpg.exe

The main reason was related to previous answers but on a different sense:

主要原因与之前的答案有关,但意义不同:

  • I was creating the gpg keys using the git (configured path) version of GPG
  • Git was configured to use the downloaded version of gpg for the commit.
  • Seems GPG implementations use their own certificate database and storage.
  • 我正在使用 GPG 的 git(配置路径)版本创建 gpg 密钥
  • Git 被配置为使用下载的 gpg 版本进行提交。
  • 似乎 GPG 实现使用自己的证书数据库和存储。

I hope this can help anyone that stumbles on this message and previous answers do not solve ther issue.

我希望这可以帮助任何偶然发现此消息的人,而之前的答案并未解决该问题。

回答by Patricio Perpetua

I had the same problem at it was that git name and emailstore in .gitconfig were differentfrom the ones of the gpg key provided. I changed them in order to match and it started to work.

我遇到了同样的问题,.gitconfig中的git名称和电子邮件存储与提供的 gpg 密钥不同。我改变了它们以匹配并且它开始工作。

回答by user6671429

Maybe you need to clone your own repository where you have rights. I had this issue when I cloned the repository of another person.

也许您需要在您拥有权限的地方克隆自己的存储库。当我克隆另一个人的存储库时,我遇到了这个问题。