当推送时出现“连接到 github.com 被远程主机关闭”的提示

15
我有一个项目,每次我使用SSH密钥(在Windows上)向我的GitHub帐户git push时,命令行会卡顿几分钟,然后最终出现错误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)

这里是我新克隆的代码库运行 git remote show origin 的结果。请注意,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)

这听起来有点像网络问题,但如果您可以从同一台机器上的另一个克隆成功地推送,那似乎不太可能。Windows 有一些神秘的文件系统行为,所以也许与其中之一有关。 - torek
你能否在Git Bash中使用GIT_SSH_COMMAND="ssh -vvv" git push origin master(或其他分支)运行失败的推送,并编辑你的问题以包含输出? - bk2204
@bk2204 我尝试使用你在Git Bash中给我的命令,成功地将代码推送到了我的masterdevelop分支。也许问题出在OpenSSH上? - Ben Rubin
2个回答

40

我无法解释长时间挂起的原因,但最终出现的 Connection to github.com closed by remote host. 错误消息可能是由于您与 GitHub 的 SSH 连接超时导致的。最近我帮助一位同事解决了类似的问题,在她的机器上 Husky 的 pre-push hook 完成所需的时间较长,当 hook 完成时,她会收到相同的 Connection to github.com closed by remote host. 错误消息。

我们发现解决方法是在她的 .ssh\config 文件中设置 ServerAliveIntervalServerAliveCountMax 值来保持连接活动状态。例如,添加以下设置将每 60 秒向服务器发送一个空包(保持连接活动状态)30 次。这将给您提供 30 分钟的连接时间。

Host *
  ServerAliveInterval 60
  ServerAliveCountMax 30

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


1
使用 Husky 的 pre-push 钩子将相同的问题与解决方案推送到 bitbucket.org。 - grantnz
对我来说,这个文件在 ~/.ssh/config 中。 - mhatch

0

wmcb91的答案的基础上,您可以在GitHub主机下明确设置时间指令。请参阅github ssh文档以在~/.ssh/config中添加您的身份信息。

Host *.github.com
    StrictHostKeyChecking yes
    IdentityFile ~/.ssh/github
    ServerAliveInterval 60
    ServerAliveCountMax 30

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接