bash - 带有 SSH 的 git clone 只能在 Git Bash 中工作,不能在 Windows CMD 上工作

标签 bash git ssh cmd git-bash

所以,我关注了this关于如何使用 Windows CMD 为 github 设置 SSH 的教程,一切正常,直到我去克隆一个 repo

git clone git@github.com:{myusername}/{myrepo}.git

我在哪里
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.

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

在摸索了一会儿之后,我决定在 git bash 上尝试一下。

我注意到的第一件事是运行
 ssh-add -l

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

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

那么,为什么只有在 git bash 上才能做到这一点,而不是在 cmd 或 powershell 上呢?这与他们似乎使用不同的 ssh 代理有关吗?如果是这种情况,我如何将它们同步在一起?

此外,当我运行以下命令时
ssh -Tv git@github.com

用我得到的cmd
debug1: identity file C:\\Users\\{myuserdirectory}/.ssh/id_rsa type 0
debug1: key_load_public: No such file or directory

但是用 git bash 我得到
debug1: identity file /c/Users/{myuserdirectory}/.ssh/id_rsa type 0


另一个区别是在 windows cmd 中我没有得到任何实例debug1: Will attempt key: ....
当我退出 git bash 并打开另一个 git bash 终端,运行 ssh-add -l再次返回 The agent has no identities.即使在我之前添加它之后,它也只在每个 session 中持续存在,这也是不可取的。

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

最佳答案

可能你是对的,他们使用不同的 ssh 代理。我遇到了完全相同的问题,这个答案对我帮助很大:
https://stackoverflow.com/a/40720527/6486458
默认情况下,git 在 C:\Program Files\Git\usr\bin 中引用自己的 ssh .我添加了 GIT_SSH环境变量并将其设置为 C:\Windows\System32\OpenSSH\ssh.exe .这可以防止 ssh 版本之间的不一致。之后,git 开始按预期从 Git Bash 和 Windows cmd 工作。
来自 git documentation :

GIT_SSH, if specified, is a program that is invoked instead of ssh when Git tries to connect to an SSH host. It is invoked like $GIT_SSH [username@]host [-p <port>] <command>.


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

关于bash - 带有 SSH 的 git clone 只能在 Git Bash 中工作,不能在 Windows CMD 上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57742378/

相关文章:

git - 撤消 git merge

git - 如何在不获取存储库的情况下 git log 远程分支

linux - 如何通过ssh连接运行PHPUnit Selenium测试?

linux - 当我在 Ubuntu 终端中执行 "-rwxr-xr-x 1 root root 920788 Mar 28 2013 bash"时,此 "ls -l"的列标题是什么

Git - 删除或编辑别名

bash - 在 Bash 脚本中随机选择并打印三个字符串之一

linux - 在不使用 root 的情况下通过 ssh 备份远程服务器

macos -/usr/bin/ssh-copy-id: line 1: ucgi::command not found

linux - 是否可以记录整个脚本?

ios - CFBundleDisplayName 中的空间与 plutil