【问题标题】:"Connection to github.com closed by remote host" when pushing推送时“远程主机关闭与 github.com 的连接”
【发布时间】:2020-07-05 01:32:18
【问题描述】:

我有一个项目,每次我使用 SSH 密钥(在 Windows 上)git push 到我的 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)

这是我新克隆的 repo 中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 有一些神秘的文件系统行为,所以可能与其中之一有关。
  • 您能否使用 GIT_SSH_COMMAND="ssh -vvv" git push origin master(或任何分支)在 Git Bash 中运行失败的推送并编辑您的问题以包含输出?
  • @bk2204 我使用您在 Git Bash 中给我的命令进行了尝试,它成功推送到了我的 masterdevelop 分支。也许问题出在 OpenSSH 上?

标签: git github git-push openssh


【解决方案1】:

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

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

Host *
  ServerAliveInterval 60
  ServerAliveCountMax 30

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

【讨论】:

  • 使用 Husky pre-push hook 推送到 bitbucket.org 的相同问题和解决方案
猜你喜欢
  • 1970-01-01
  • 2020-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多