Ben Rubin
Ben Rubin

Reputation: 7341

"Connection to github.com closed by remote host" when pushing

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)

Upvotes: 18

Views: 24964

Answers (2)

wmcb91
wmcb91

Reputation: 471

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.

Upvotes: 45

Richard Tyler Miles
Richard Tyler Miles

Reputation: 685

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

Upvotes: 0

Related Questions