众所周知,GitHub是国外网站,加上运营商DNS经常抽风污染,这GitHub是经常无法访问,就算打开了可能下载速度极慢,不能愉快的Ctrl+C、 Ctrl+V, 令人抓狂。下面就介绍几种常见的加速方式。

一、通过ghproxy加速

在任何你要下载的仓库前面加上https://ghproxy.com/
比如你要下载的是https://github.com/spring/spring.git, 那么只需要把地址改成https://ghproxy.com/https://github.com/spring/spring.git, 然后git clone就能实现加速了。

ghproxy支持终端命令行 git clone , wget , curl 等工具下载.
支持 raw.githubusercontent.com , gist.github.com , gist.githubusercontent.com 文件下载.
注意:不支持 SSH Key 方式 git clone 下载.

二、通过代理加速

这种方式的前提是你得有个代理,Git有两种下载方式,代理方式也有两种

1. Http Proxy

$ git config --global http.proxy socks5://127.0.0.1:7890
或者直接修改.gitconfig文件

1
2
[http]
proxy = socks5h://127.0.0.1:7890

sock5是代理的协议,h表示host解析也走这个代理,
如果你的代理只支持http代理,那么要把sock5改成http

2. SSH Proxy

很多人喜欢用ssh方式下载代码,这种方式用上面的http方式代理是无效的。需要在~/.ssh/config配置ssh代理

1
2
3
4
5
6
$ cat ~/.ssh/config

Host github.com
Hostname github.com
IdentityFile /xxx/.ssh/github_id_rsa
ProxyCommand nc -v -x 127.0.0.1:7890 %h %p

nc是Linux和macos下的网络命令工具,Windows上对应的是connect.exe,安装Windows Git附带

1
2
3
4
5
6
7
8
$ connect
connect --- simple relaying command via proxy.
Version 1.105
usage: C:\Program Files\Git\mingw64\bin\connect.exe [-dnhst45] [-p local-port]
[-H proxy-server[:port]] [-S [user@]socks-server[:port]]
[-T proxy-server[:port]]
[-c telnet-proxy-command]
host port

win的写法

1
2
3
4
5
6
$ cat ~/.ssh/config

Host github.com
Hostname github.com
IdentityFile /c/users/xxx/.ssh/github_id_rsa
ProxyCommand connect -S 127.0.0.1:7890 %h %p