16

I have a project where every time I git push to my GitHub account using SSH keys (on Windows), the command line hangs for several minutes, and then I eventually get the error Connection to github.com closed by remote host. I can do git pull or git fetch successfully. I can also do ssh -T [email protected] successfully.

I've been pushing successfully to this project for a while. I think this problem started when I switched to use OpenSSH as my SSH agent and configured it to use two different keys for different SSH accounts. However, I've disabled the separate keys (I renamed my .ssh\config file) to test, and I still have the same problem.

I tried cloning this project to another location on my computer, updating it, and doing a git push and that works correctly from the newly cloned repository.

Here are the results of git remote show origin from my original repo.

* 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)

Here are the results of git remote show origin from my newly cloned repo. Note that the test branch is a new branch that I created so I didn't overwrite 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)
3
  • This kind of sounds like a networking issue, but if you can push successfully from another clone on the same machine, that seems less likely. Windows has some mysterious file system behaviors, so maybe it's something to do with one of those.
    – torek
    Mar 24, 2020 at 18:53
  • Can you run the failing push in Git Bash with GIT_SSH_COMMAND="ssh -vvv" git push origin master (or whatever branch) and edit your question to include the output?
    – bk2204
    Mar 24, 2020 at 23:11
  • @bk2204 I tried it using the command you gave me in Git Bash and it pushed successfully on both my master and develop branch. Maybe the problem is with OpenSSH?
    – Ben Rubin
    Mar 25, 2020 at 1:37

2 Answers 2

40

I can't explain the long hang time, but the eventual Connection to github.com closed by remote host. message is likely caused by your SSH connection with GitHub timing out. I recently helped a coworker solve a similar issue where our Husky pre-push hook was taking a long time to complete on her machine. By the time the hook finished, she received the same Connection to github.com closed by remote host. message.

We found the solution was keeping her connection alive by setting values for ServerAliveInterval and ServerAliveCountMax in her .ssh\config file. For example, adding the following settings would send a null packet to the server every 60 seconds (keeping the connection alive) for 30 rounds. This would buy you 30 minutes of connection.

Host *
  ServerAliveInterval 60
  ServerAliveCountMax 30

You can adjust the the values however you see fit for your use.

2
  • 1
    Same problem & solution pushing to bitbucket.org with a Husky pre-push hook
    – grantnz
    Oct 29, 2021 at 3:11
  • For me this file was in ~/.ssh/config
    – mhatch
    May 11, 2022 at 14:51
0

Building off of wmcb91's answer, you can explicitly set the time directives under the GitHub host. See the github ssh documentation for adding your identity info in ~/.ssh/config

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.