【问题标题】:git clone with SSH only working in Git Bash not on Windows CMD带有 SSH 的 git clone 只能在 Git Bash 中工作,不能在 Windows CMD 上工作
【发布时间】:2020-01-04 15:08:56
【问题描述】:

所以,我遵循了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 密钥?

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

那么,为什么只有在 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.,就像它只在每个会话中持续存在,这也是不可取的。

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

【问题讨论】:

    标签: bash git ssh cmd git-bash


    【解决方案1】:

    您的 ssh-agent 似乎没有运行或无法识别您的 ssh 密钥

    试试这个:

    # add the default ~/.ssh keys to the ssh-agent
    ssh-add
    
    # restart the ssh-agent
    eval $(ssh-agent)
    

    # On windows:
    start-ssh-agent
    

    ssh-add

    ssh-add 将 RSA 或 DSA 身份添加到身份验证代理 ssh-agent。
    不带参数运行时,它会添加文件~/.ssh/id_rsa~/.ssh/id_dsa~/.ssh/identity

    可以在命令行中给出其他文件名

    【讨论】:

    • 这就是我用于 git bash 的,但我在 cmd 中得到'eval' is not recognized as an internal or external command,
    • 在 git bash 中执行它并再次尝试 CMD。结果如何?
    • 我已经用start-ssh-agent更新了我的windows答案
    • 尝试在 bash 中运行并在 CMD 中执行,结果相同。此外,运行start-ssh-agent 然后运行ssh-add -l 将返回Error connecting to agent: No such file or directory。我不知道为什么?
    • 在 powershell 中也没有乐趣,尝试克隆 git repo 时仍然得到相同的结果...
    【解决方案2】:

    您可能是对的,他们使用了不同的 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,如果指定,是一个被调用的程序,而不是 ssh 当 Git 尝试连接到 SSH 主机时。它的调用方式类似于$GIT_SSH [username@]host [-p <port>] <command>

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

    【讨论】:

      【解决方案3】:

      如果您安装 Git bash,Windows 上会出现一个奇怪的错误。打开命令提示符,然后执行

      ls ~/.ssh
      

      如果您发现此文件夹已创建,则将公钥和私钥从您的用户文件夹复制到此路径:

      cp C:\Users\username\.ssh\id_* ~/.ssh/
      

      由于某种原因,Windows 命令提示符会在您第一次执行 git 克隆时创建此路径,之后它只请求 git@gitlab / git@github 密码。

      【讨论】:

        猜你喜欢
        • 2022-07-05
        • 1970-01-01
        • 2012-11-20
        • 1970-01-01
        • 1970-01-01
        • 2014-09-14
        • 2015-12-12
        相关资源
        最近更新 更多