推送时“远程主机关闭与 github.com 的连接”

Ben*_*bin 4 git openssh github git-push

我有一个项目,每次我git push使用 SSH 密钥(在 Windows 上)访问我的 GitHub 帐户时,命令行都会挂起几分钟,然后我最终会得到Connection to github.com closed by remote host. 我可以做git pullgit fetch成功的错误。我也可以ssh -T git@github.com成功。

我已经成功地推动了这个项目一段时间了。我认为当我切换到使用 OpenSSH 作为我的 SSH 代理并将其配置为对不同的 SSH 帐户使用两个不同的密钥时,这个问题就开始了。但是,我已经禁用了单独的键(我重命名了我的.ssh\config文件)进行测试,但我仍然遇到同样的问题。

我尝试将这个项目克隆到我计算机上的另一个位置,更新它,然后git push从新克隆的存储库中正确运行。

这是git remote show origin我原始回购的结果。

* remote origin
  Fetch URL: git@github.com:MyUserName/MyRepo.git
  Push  URL: git@github.com:MyUserName/MyRepo.git
  HEAD branch: master
  Remote branches:
    develop tracked
    master  tracked
    test    new (next fetch will store in remotes/origin)
  Local branches configured for 'git pull':
    develop merges with remote develop
    master  merges with remote master
  Local refs configured for 'git push':
    develop pushes to develop (fast-forwardable)
    master  pushes to master  (fast-forwardable)
Run Code Online (Sandbox Code Playgroud)

这是git remote show origin我新克隆的 repo的结果。请注意,该test分支是我创建的一个新分支,因此我没有覆盖master.

* remote origin
  Fetch URL: git@github.com:MyUserName/MyRepo.git
  Push  URL: git@github.com:MyUserName/MyRepo.git
  HEAD branch: master
  Remote branches:
    develop tracked
    master  tracked
    test    tracked
  Local branches configured for 'git pull':
    master merges with remote master
    test   merges with remote test
  Local refs configured for 'git push':
    master pushes to master (up to date)
    test   pushes to test   (up to date)
Run Code Online (Sandbox Code Playgroud)

小智 9

我无法解释长时间的挂起时间,但最终的Connection to github.com closed by remote host.消息可能是由于您与 GitHub 的 SSH 连接超时造成的。我最近帮助一位同事解决了一个类似的问题,即我们的 Husky pre-push hook 在她的机器上需要很长时间才能完成。当钩子结束时,她收到了同样的Connection to github.com closed by remote host.信息。

我们发现解决方案是通过在她的文件中设置ServerAliveInterval和 的值来保持她的连接。例如,添加以下设置将每 60 秒(保持连接活动)向服务器发送一个空数据包,持续 30 轮。这将为您带来 30 分钟的连接时间。ServerAliveCountMax.ssh\config

Host *
  ServerAliveInterval 60
  ServerAliveCountMax 30
Run Code Online (Sandbox Code Playgroud)

您可以根据自己的需要调整这些值。