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

问题描述 投票:0回答:2

我有一个项目,每次我使用 SSH 密钥(在 Windows 上)

git push
到我的 GitHub 帐户时,命令行挂起几分钟,然后我最终得到错误
Connection to github.com closed by remote host.
我可以做
git pull
 git fetch
成功。我也能做到
ssh -T [email protected]
成功

一段时间以来,我一直在成功推动这个项目。我认为当我切换到使用 OpenSSH 作为我的 SSH 代理并将其配置为对不同的 SSH 帐户使用两个不同的密钥时,这个问题就开始了。但是,我已经禁用了单独的键(我重命名了我的

.ssh\config
文件)来测试,但我仍然有同样的问题。

我尝试将这个项目克隆到我计算机上的另一个位置,更新它,然后做一个

git push
并且从新克隆的存储库中可以正常工作。

这是

git remote show origin
的结果来自我的原始回购。

* remote origin
  Fetch URL: [email protected]:MyUserName/MyRepo.git
  Push  URL: [email protected]: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)

这是

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

* remote origin
  Fetch URL: [email protected]:MyUserName/MyRepo.git
  Push  URL: [email protected]: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)
git github git-push openssh
2个回答
39
投票

我无法解释长时间挂起,但最终的

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

我们发现解决方案是通过在她的

ServerAliveInterval
文件中设置
ServerAliveCountMax
.ssh\config
的值来保持她的联系。例如,添加以下设置将每 60 秒(保持连接活动)向服务器发送一个空数据包,持续 30 轮。这将为您提供 30 分钟的连接时间。

Host *
  ServerAliveInterval 60
  ServerAliveCountMax 30

您可以调整您认为适合您使用的值。


0
投票

基于 wmcb91 的答案,您可以在 GitHub 主机下明确设置时间指令。请参阅github ssh 文档 以在

~/.ssh/config

中添加您的身份信息
Host *.github.com
    StrictHostKeyChecking yes
    IdentityFile ~/.ssh/github
    ServerAliveInterval 60
    ServerAliveCountMax 30
© www.soinside.com 2019 - 2024. All rights reserved.