Git配置代理


做技术的难免需要经常从github或者gitlab等等国外的代码托管平台上拉取和下载远程的仓库内容,但由于github配置的托管仓库的服务器地址在国内速度堪称龟速,不是小水龙头般的下载速度就是出现unable to access '...' Couldn't resolve host '...'等错误,为此,git提供了相关代理的功能:

举个栗子

当使用https方式,如:

1
$ git clone https://github.com/kubernetes/kubernetes.git

使用ssh+git方式:

1
$ git clone git@github.com:kubernetes/kubernetes.git

根据自身习惯可以选择配置针对其中一种的代理方式,也可以两者皆可,但要注意,当使用config等非临时的写入配置文件的代理配置方式,这是全局的配置方式;需要更多用于控制代理与个性化的参数请自行查阅官方git文档。

Git ssh+git

ssh代理设置

注意:实际上,当我们使用ssh+git方式的时候,我们完全不必在~/.gitconfig中配置任何代理地址或者运行git config --global http.proxy ...与其他相似的命令。

~/ssh/config中配置Proxy,git在拉取的时候就会开始使用此代理。

socks代理

当代理服务端开启socks服务时,我们可以使用nc命令,使用sshProxyCommand配合nc可以让ssh通过设置的代理访问服务器

1
$ vim ~/.ssh/config

然后写入一些命令:

1
2
Host *
ProxyCommand nc -X 5 -x 127.0.0.1:1080 %h %p

nc命令上述涉及的参数:-X是指定协议参数:4即为socks4协议,5即为socks5协议

			   **-x**是指定代理服务器及端口 [代理服务器:端口]

非socks代理

没有socks代理的时候,可以使用corkscrew可以让ssh使用HTTP代理,Crokscrew是一个HTTP隧道代理ssh的工具

如果你的系统没有安装corkscrew,你可以通过以下方式安装:

对于mac用户:

1
$ brew install crokscrew

对于linux用户:

1
$ sudo apt-get install crokscrew

对于windows用户有更方便的管理ssh的工具,如Putty等,都可对ssh直接设置代理, 当然mac端也有termius这样的图形界面工具,根据自身喜好决定配置思路

使用查询路径的命令查询crokscrew的执行文件路径如:

1
$ which crokscrew

假设此处查到的路径为%crokscrew_path%,向~/.ssh/config文件中添加如下内容:

1
2
3
4
5
Host github.com
Hostname github.com
ServerAliventerval 55
ForwardAgent yes
ProxyCommand %crokscrew_path% 127.0.0.1 7580 %h %p

127.0.0.1:7580即代理服务端地址及端口,其中可以只添加最后一句,前面的语句都是用于控制一些参数和作用域名,关于如何详细配置git的参数可参阅:git-config

Git http

HTTP代理配置

使用~/.gitconfig文件全局的配置代理,使用-global参数切换,或者在.git/config文件中设置对某个仓库的局部代理

设置一个全局代理

配置一个全局代理当你想使用它访问所有的远程仓库:

配置HTTP代理:

1
$ git config --global http.proxy http://proxyUsername:proxyPassword@proxy.server.com:port

配置HTTPS代理:

1
$ git config --global https.proxy http://proxyUsername:proxyPassword@proxy.server.com:port

配置socks5代理:

1
2
$ git config --global http.proxy socks5://proxyUsername:proxyPassword@proxy.server.com:port
$ git config --global https.proxy socks5://proxyUsername:proxyPassword@proxy.server.com:port

用户名和密码为缺省状态:

1
2
3
4
$ git config --global http.proxy http://proxy.server.com:port
$ git config --global https.proxy http://proxy.server.com:port
$ git config --global http.proxy socks5://proxy.server.com:port
$ git config --global https.proxy socks5://proxy.server.com:port

注意:这种方法配置的代理是临时代理,当配置的终端关闭,则会失效,修改配置文件则是永久变更,修改后最好重启git来使设置生效。

配置针对特定URL代理

当希望能够指定代理应该用于那些被http.<url>.key所指定的URL:

1
$ git config --global http.https://domain.com.proxy http://proxyUsername:proxyPassword@proxy.server.com:port
1
$ git config --global https.https://domain.com.rpoxy https://proxyUsername:proxyPassword@proxy.server.com:port

同理,同样的命令在~/.gitconfig文件中可以添加为:

1
2
3
4
5
6
7
[http]
[http "https://domain.com"]
proxy = http://proxyUsername:proxyPassword@proxy.server.com:port

[https]
[https "https://domain.com"]
proxy = http://proxyUsername:proxyPassword@proxy.server.com:port

处理后续SSL报错

如果在配置代理之后还是会遇到unable to access 'https://...': Unknown SSL protocol error in connection to ...:443,或许你可以使用-c http.sslVerify=false参数来手动关闭SSL验证,譬如:

1
$ git -c http.sslVerify=false clone https://domain.com/path/to/git

可以选择在拉取下来的项目中配置.git/config,在该项目文件夹打开终端:

1
$ git config http.sslVerify false

当仅仅想针对特定URL使用http.<url>.sslVerify设置而其他仍然使用全局配置时:

1
2
$ git config --global http.https://domain.com.proxy http://proxyUsername:proxyPassword@proxy.server.com:port
$ git config --global http.https://domain.com.sslVerify false

~/.gitconfig文件根据同样的要求配置的结果:

1
2
3
4
[http]
[http "https://domain.com"]
proxy = http://proxyUsername:proxyPassword@proxy.server.com:port
sslVerify = false

显示当前设置

显示当前所有http部分当前配置:

1
$ git config --global --get-regexp http.*

同理显示https部分当前配置:

1
$ git config --global --get-regexp https.*

如果远程项目已经被拉取到本地了,进入项目目录,丢弃--global参数同样能查询当前所有配置

1
$ git config --get-regexp http.*

取消代理或SSL验证的配置

当在终端中使用git config配置代理,在终端关闭之后,就会自动失效,若要配置长期有效的请将配置命令写入~/.gitconfig文件。

使用--unset参数来取消这些特定的代理配置,比如http.proxyhttp.<url>.proxy代理移除:

1
2
3
4
5
$ git config --global --unset http.proxy
$ git config --global --unset http.https://domain.com.proxy

$ git config --global --unset http.sslVerify
$ git config --global --unset http.https://domain.com.sslVerify

查看配置信息:

1
$ git config -l --global

执行查看代理:

1
$ git config -l