Git 克隆/拉动持续冻结在 "Store key in cache?"

Git clone / pull continually freezing at "Store key in cache?"

我正在尝试从我的 BitBucket 帐户克隆一个存储库到我的 Windows 10 笔记本电脑 (运行 GitBash)。我已经完成了连接所需的所有步骤(设置我的 SSH 密钥,通过成功 SSHing git@bitbucket.org 等进行验证)。但是,每当我尝试克隆一个存储库时,在确认我要缓存 Bitbucket 的密钥后,提示会不断挂断。

User@Laptop MINGW64 /C/Repos
$ git clone git@bitbucket.org:mygbid/test.git
Cloning into 'test'...
The server's host key is not cached in the registry. You
have no guarantee that the server is the computer you
think it is.
The server's rsa2 key fingerprint is:
ssh-rsa 2048 97:8c:1b:f2:6f:14:6b:5c:3b:ec:aa:46:46:74:7c:40
If you trust this host, enter "y" to add the key to
PuTTY's cache and carry on connecting.
If you want to carry on connecting just once, without
adding the key to the cache, enter "n".
If you do not trust this host, press Return to abandon the
connection.
Store key in cache? (y/n) y

没有文件被克隆,结果是一个空的 repo。尝试从这个 repo 启动 git pull origin master 也要求缓存密钥,然后挂起而没有反馈。尽管我在测试 SSH 时没有要求缓存密钥,但 git 操作总是在每次失败前要求密钥。

没有任何错误消息可供使用,我真的不知道哪里出了问题。我尝试了多个回购协议,包括非常小的回购协议,但都没有成功。

我在 Windows 10 上克隆存储库时也遇到了这个问题。

我通过使用 Putty GUI 通过 SSH 连接到有问题的服务器(在您的情况下:bitbucket.org)解决了这个问题,然后在提示询问您是否要保存服务器时单击 'Yes'缓存的关键。 运行 克隆命令再次对我有用!

  1. 打开腻子
  2. 输入主机名(如bitbucket.org
  3. 点击打开
  4. 在弹出窗口中单击“是”以缓存主机密钥
  5. 关闭 Putty

要从 powershell 执行此操作,请打开 powershell window 并粘贴以下内容:

echo y | & 'C:\Program Files (x86)\GitExtensions\PuTTY\plink.exe' -ssh git@github.com
echo y | & 'C:\Program Files (x86)\GitExtensions\PuTTY\plink.exe' -ssh git@gist.github.com
echo y | & 'C:\Program Files (x86)\GitExtensions\PuTTY\plink.exe' -ssh git@bitbucket.org

或使用 PuTTY 独立版本:

echo y | & 'C:\Program Files (x86)\PuTTY\plink.exe' -ssh git@github.com
echo y | & 'C:\Program Files (x86)\PuTTY\plink.exe' -ssh git@gist.github.com
echo y | & 'C:\Program Files (x86)\PuTTY\plink.exe' -ssh git@bitbucket.org

另外值得一提的是 putty stores known hosts under a registry key:

HKEY_CURRENT_USER\SoftWare\SimonTatham\PuTTY\SshHostKeys

要简化上述内容,您可以将以下内容放入 .reg 文件中,然后 运行 它:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\SOFTWARE\SimonTatham\PuTTY\SshHostKeys]
"rsa2@22:github.com"="0x23,0xab603b8511a67679bdb540db3bd2034b004ae936d06be3d760f08fcbaadb4eb4edc3b3c791c70aae9a74c95869e4774421c2abea92e554305f38b5fd414b3208e574c337e320936518462c7652c98b31e16e7da6523bd200742a6444d83fcd5e1732d03673c7b7811555487b55f0c4494f3829ece60f94255a95cb9af537d7fc8c7fe49ef318474ef2920992052265b0a06ea66d4a167fd9f3a48a1a4a307ec1eaaa5149a969a6ac5d56a5ef627e517d81fb644f5b745c4f478ecd082a9492f744aad326f76c8c4dc9100bc6ab79461d2657cb6f06dec92e6b64a6562ff0e32084ea06ce0ea9d35a583bfb00bad38c9d19703c549892e5aa78dc95e250514069"
"rsa2@22:gist.github.com"="0x23,0xab603b8511a67679bdb540db3bd2034b004ae936d06be3d760f08fcbaadb4eb4edc3b3c791c70aae9a74c95869e4774421c2abea92e554305f38b5fd414b3208e574c337e320936518462c7652c98b31e16e7da6523bd200742a6444d83fcd5e1732d03673c7b7811555487b55f0c4494f3829ece60f94255a95cb9af537d7fc8c7fe49ef318474ef2920992052265b0a06ea66d4a167fd9f3a48a1a4a307ec1eaaa5149a969a6ac5d56a5ef627e517d81fb644f5b745c4f478ecd082a9492f744aad326f76c8c4dc9100bc6ab79461d2657cb6f06dec92e6b64a6562ff0e32084ea06ce0ea9d35a583bfb00bad38c9d19703c549892e5aa78dc95e250514069"
"rsa2@22:bitbucket.org"="0x23,0xb9b88df3578371a7eb80c78bcda14fb30da436f11ca932a5fd5a8b6adfcc681df7a59cb4cb7ac966d9eac11daa38ebdbc0a6582a210ed4ee95a8d101c4abc925e942ab47535d64f9a5b3b68035c2ea1e900d709a1e8ea938718f532f9805a190446b92bac3040126225ae9d8374bc2008f106979d631734c7453f78c70091f4783b288869cb3c1941a784cd9baad823be27333833dc1f488a45b85952be75cf0a64965662302e3915378dcd5cfcd3ec903d804a29dff2fdf19df5deba4534b09e4dea6e44f152e339b3c43be98ddadfc56533192e216a3d673f00b4aa9cc9e7870acd8b6adb7e0feb77f2292fc2dede94819def3eb1e785541a06ab31ccf725f"

putty-hosts.reg gist

为了解决这个问题,我将 GitBash 配置为使用带有 -batch 选项的 plink。该选项禁用所有提示 - plink 将终止而不会挂起,并且不会将任何密钥指纹添加到缓存中。

要将 -batch 参数添加到 GitBash 执行的 plink 命令,您可以设置 git 配置选项:

git config --global core.sshCommand "plink -batch"

或设置GIT_SSH_COMMAND环境变量。

从未知主机克隆 repo 时的输出类似于:

The server's host key is not cached in the registry. You
have no guarantee that the server is the computer you
think it is.
The server's rsa2 key fingerprint is:
ssh-rsa 2048 97:8c:1b:f2:6f:14:6b:5c:3b:ec:aa:46:46:74:7c:40
Connection abandoned.
fatal: Could not read from remote repository.

在此消息之后,您可以使用以下命令将密钥添加到缓存:

echo y | plink git@bitbucket.org

REMARK:请检查 plink 是否在您的 PATH 中。或者在 GitBash 配置选项中使用类 UNIX 路径,例如:

/c/Program\ Files/PuTTY/plink.exe -batch

这听起来有点傻,但在尝试了以上所有方法后,我决定使用默认选项重新安装 Git Bash 并且成功了。

即使在执行了其他答案中提到的解决方法之后,您也可能会遇到如下错误:

FATAL ERROR: Disconnected: No supported authentication methods available (server sent: publickey)

要同时解决这两个问题,通过将以下内容添加到您的 ~/.profile 文件 (C:\Users\<Username>\.profile),将 git bash 更改为使用 SSH 而不是 PuTTY。如果您还没有此文件,请使用此行创建一个新文件。

GIT_SSH="/usr/bin/ssh.exe"

然后打开一个新的 git bash window 并再次尝试您的 git clonegit pull

请注意,如果您还没有 SSH 密钥,这可能需要您创建一个。为此,请按照 Bitbucket 站点上的说明进行操作。

有关相关信息,请参阅 this SO question

我设法通过 运行 plink 直接让它工作,在选美 运行 之后直接使用 plink 命令 - plink.exe -agent -v git@github.com 然后在这 git 之后工作而不挂起.

在您的 git bash shell 中,检查 GIT_SSH 是否存在:
echo $GIT<tab><tab>
如果存在并且设置为putty,执行:
unset GIT_SSH
您可能希望将其放入 git bash 启动脚本之一。
这不是一个通用的解决方案。它在我们的特殊情况下有效。

如果你使用KiTTY (instead of PuTTY), it has -auto-store-sshkey argument.

因此,您可以将 GIT_SSH_COMMAND(或 git config --global core.sshCommand)设置为 c:/KiTTY/klink.exe -auto-store-sshkey

输出仍然包含有关新密钥和问题的信息,但不会等待答案:

The server's host key is not cached in the registry. You
have no guarantee that the server is the computer you
think it is.
The server's ssh-ed25519 key fingerprint is:
ssh-ed25519 255 2e:65:6a:c8:cf:bf:b2:8b:9a:bd:6d:9f:11:5c:12:16
If you trust this host, enter "y" to add the key to
PuTTY's cache and carry on connecting.
If you want to carry on connecting just once, without
adding the key to the cache, enter "n".
If you do not trust this host, press Return to abandon the
connection.
Store key in cache? (y/n)
Autostore key is on

要连接的默认 SSH 端口是 7999