不用代理, 解决 Github ssh 协议方式超时失败解决方法

建议点击 查看原文 查看最新内容。

原文链接: https://gist.github.com/Tamal/1cc77f88ef3e900aeae65f0e5e504794

在使用 ssh 协议的时候, 访问超市失败 ssh: connect to host github.com port 22: Connection timed out

1
2
3
4
5
6
7
8
$ git clone [email protected]:xxxxx/xxxx.git my-awesome-proj
Cloning into 'my-awesome-proj'...
ssh: connect to host github.com port 22: Connection timed out
fatal: Could not read from remote repository.

$ # This should also timeout
$ ssh -T [email protected]
ssh: connect to host github.com port 22: Connection timed out

但是访问 ssh.github.com 正常

1
2
3
$ # but this might work
$ ssh -T -p 443 [email protected]
Hi xxxx! You've successfully authenticated, but GitHub does not provide shell access.

配置 .ssh/config 文件

添加一下内容

1
2
3
4
5
6
7
# # Override SSH settings
# vim ~/.ssh/config
# Add section below to it
# https://gist.github.com/Tamal/1cc77f88ef3e900aeae65f0e5e504794
Host github.com
  Hostname ssh.github.com
  Port 443

重新测试, 发现可以用了。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$ ssh -T [email protected]
Hi xxxxx! You've successfully authenticated, but GitHub does not
provide shell access.

$ git clone [email protected]:xxxxxx/xxxxx.git my-awesome-proj
Cloning into 'my-awesome-proj'...
remote: Enumerating objects: 15, done.
remote: Counting objects: 100% (15/15), done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 15 (delta 0), reused 15 (delta 0), pack-reused 0
Receiving objects: 100% (15/15), 22.90 KiB | 4.58 MiB/s, done.

HTTPS 443 超时

非自己的项目,不能使用 ssh 协议。

使用 HTTPS 443 端口超时

1
2
3
4
$ git clone https://github.com/liaoliaots/nestjs-redis.git

正克隆到 'nestjs-redis'...
fatal: 无法访问 'https://github.com/liaoliaots/nestjs-redis.git/':Failed to connect to github.com port 443 after 75011 ms: Couldn't connect to server

在配置了 ssh 优化的基础上, 再在 ~/.gitconfig 中配置替换规则

[url "[email protected]:"]
    insteadOf = https://github.com/

再次测试, 成功

1
2
3
4
5
6
7
8
$ git clone https://github.com/liaoliaots/nestjs-redis.git
正克隆到 'nestjs-redis'...
remote: Enumerating objects: 4562, done.
remote: Counting objects: 100% (138/138), done.
remote: Compressing objects: 100% (80/80), done.
remote: Total 4562 (delta 90), reused 83 (delta 57), pack-reused 4424 (from 1)
接收对象中: 100% (4562/4562), 3.85 MiB | 2.23 MiB/s, 完成.
处理 delta 中: 100% (2961/2961), 完成.