Github SSH 工作但使用 ssh 克隆失败

Github SSH works but clone using ssh fails

我在使用 ssh 克隆 git 存储库时遇到了一个奇怪的问题。我有 ssh 设置:

ssh -T git@github.com
Hi yusufali2205! You've successfully authenticated, but GitHub does not provide shell access.

我使用的是正确的克隆 url 并且可以访问我想要克隆的存储库。但是出现错误:

➤ git clone git@github.com:<some-org>/<repo>.git
Cloning into 'project'...
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

没有其他消息可以调试我的 ssh 设置或 git 配置有什么问题。

我发现我的 .gitconfig 中有一个条目正在用 https 替换 ssh。

[url "https"]
    insteadOf = git

我可能在使用某些工具时不小心添加了这个条目。所以克隆命令实际上是使用 url https@github.com:<some-org>/<repo>.git

.gitconfig 中删除上述条目后,问题得到解决。

在我的例子中,我必须删除 ~/.ssh/known_hosts 文件,以便在克隆时重新制作该文件。在此之后,它起作用了

我遇到了同样的问题并进行了研究我尝试了一些东西并且我明白了。转到您的 .ssh 目录,用记事本打开文件 known_hosts,就在它的开头 "github.com" ...插入 SSH: 在 github.com 的开头,它看起来像this: "SSH: github.com" 并保存,再试一次。它对我有用!

我遇到了同样的问题,但就我而言,这是因为我使用自定义名称(例如 my_keymy_key.pub)命名了我的密钥文件,而不是让 ssh-keygen 使用默认值,它们是:

  • id_rsa
  • id_dsa
  • id_ecdsa
  • id_ed25519
  • id_ed25519_sk
  • id_xmss

如果您使用 ssh-add 添加自定义命名键(正如许多教程所说的那样),那么命令 ssh -T git@github.com 可以完美运行,但 git clone 命令则不行。
这是因为 git 在使用 ssh 时仅查找具有默认名称的密钥。

我能够通过向全局 .gitconfig 添加以下 属性:

来确定这一点
[core]
    sshCommand = ssh -vvv

当您尝试使用 ssh 克隆存储库(或其他 git 操作)时,基本上会吐出所有日志消息。

注意:我在 Windows 10,可能这个问题也与如何 git 在 Windows。我没有在 *nix 系统上测试过这个。