使用 SSH 的 git clone 只能在 Git Bash 中工作,不能在 Windows CMD 上工作

mcc*_*osa 2 git ssh bash cmd git-bash

因此,我遵循了有关如何使用 Windows CMD 为 github 设置 SSH 的教程,一切都工作正常,直到我使用以下命令克隆存储库

git clone git@github.com:{myusername}/{myrepo}.git
Run Code Online (Sandbox Code Playgroud)

我从哪里得到

git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
Run Code Online (Sandbox Code Playgroud)

即使当我运行时ssh -T git@github.com,我也会收到预期的消息,告诉我我已通过身份验证。

绞尽脑汁后,我决定在 git bash 上尝试一下。

我注意到的第一件事是跑步

 ssh-add -l
Run Code Online (Sandbox Code Playgroud)

在 git bash 中,我得到了The agent has no identities.,但是当我在 Windows CMD 上运行相同的命令时,我得到了所有 SSH 密钥?

因此,在 git bash 中添加 ssh 密钥后,我能够克隆我的存储库。

那么,为什么我只能在 git bash 上执行此操作,而不能在 cmd 或 powershell 上执行此操作?这是否与他们使用不同的 ssh 代理有关?如果是这种情况,我怎样才能将它们同步在一起?

此外,当我运行以下命令时

ssh -Tv git@github.com
Run Code Online (Sandbox Code Playgroud)

用我得到的命令

debug1: identity file C:\\Users\\{myuserdirectory}/.ssh/id_rsa type 0
debug1: key_load_public: No such file or directory
Run Code Online (Sandbox Code Playgroud)

但通过 git bash 我得到

debug1: identity file /c/Users/{myuserdirectory}/.ssh/id_rsa type 0

Run Code Online (Sandbox Code Playgroud)

另一个区别是在 Windows cmd 中我没有得到任何实例 debug1: Will attempt key: ....

当我退出 git bash 并打开另一个 git bash 终端并ssh-add -l再次运行时,The agent has no identities.即使我之前添加了它,它也会返回,就好像它只在每个会话中持续存在,这也是不可取的。

任何对此的帮助将不胜感激!

小智 5

也许你是对的,他们使用了不同的 ssh 代理。我遇到了完全相同的问题,这个答案对我帮助很大: https ://stackoverflow.com/a/40720527/6486458

默认情况下 git 引用它自己的 ssh C:\Program Files\Git\usr\bin。我添加了GIT_SSH环境变量并将其设置为C:\Windows\System32\OpenSSH\ssh.exe. 这可以防止 ssh 版本之间不一致。之后,git 开始按照 Git Bash 和 Windows cmd 的预期工作。

来自 git文档

GIT_SSH,如果指定的话,是当 Git 尝试连接到 SSH 主机时调用的程序,而不是 ssh。它的调用方式类似于$GIT_SSH [username@]host [-p <port>] <command>.

另请参阅此答案:https ://stackoverflow.com/a/8713121/6486458