git配置代理

在使用GitHub时,由于国内网络对GitHub不太友好,经常在进行clone项目时速度特别慢,这时候我们就需要通过设置代理的方式来加速git的下载速度。

git配置代理有两种方式,一种是全局代理,一种是针对指定地址进行代理。

全局代理会将所有的网络请求转发到代理服务器,对于一些不需要经过代理的仓库请求,可能会拖慢速度。而针对链接地址的代理方式就不存在这种缺陷。

设置代理之前,你需要一台代理服务器,并且本地代理客户端可以正常访问代理服务器。

首先确认代理服务器使用的网络代理方式,主流的有socks5http代理,记住正在使用的代理方式对应的端口,一下命令中需要使用。

配置全局代理

1
2
3
4
5
6
7
# socks5协议,1080端口修改成自己的本地代理端口
git config --global http.proxy socks5://127.0.0.1:1080
git config --global https.proxy socks5://127.0.0.1:1080

# http协议,8001端口修改成自己的本地代理端口
git config --global http.proxy http://127.0.0.1:8001
git config --global https.proxy https://127.0.0.1:8001

局部代理

1
2
3
4
5
6
7
# socks5协议,1080端口修改成自己的本地代理端口
git config --global http.https://github.com.proxy socks5://127.0.0.1:1080
git config --global https.https://github.com.proxy socks5://127.0.0.1:1080

# http协议,8001端口修改成自己的本地代理端口
git config --global http.https://github.com.proxy https://127.0.0.1:8001
git config --global https.https://github.com.proxy https://127.0.0.1:8001

重置代理

1
2
3
4
5
6
7
# 重置全局代理
git config --global --unset http.proxy
git config --global --unset https.proxy

# 重置指定代理
git config --global --unset http.https://github.com.proxy
git config --global --unset https.https://github.com.proxy