1. 问题描述
但凡要去国外搞点东西回来,这网络就慢的不行。还要有各种加速方法,让我们可以把东西顺利的搞回来。当然,我们这里并不是要介绍这些方法,而是要介绍如何使用,并且每种工具使用这些方法的方式都是不同的(技术文章的断句事件非常痛苦的事情)。
在到 GitHub 拉取那些纯净无害的代码时,我们就会遇到这种网络问题。这时候我们就需要配置 Git 来使用网络加速服务,以提高代码拉取速度。
该笔记将记录:在 Gti 中,如何使用网络加速服务,以提高代码拉取速度,以及相关问题处理。
2. 解决方案
2.1. 持久配置
修改配置文件:
# 配置当前仓库,以及取消配置
git config http.proxy 'socks5://127.0.0.1:7070' # 通过袜子协议:-)
git config --unset http.proxy
# 全局配置,以及取消配置
git config --global http.proxy 'http://127.0.0.1:8123'
git config --global --unset http.proxy
注意事项: 1)选项 http.proxy 适用于 HTTP 与 HTTPS 协议; 2)对于那些调用 Git 命令来拉取代码的程序,使用 全局配置 方式基本是唯一的选择;
2.2. 临时配置
仅对本次克隆有效:
# 方法一、命令行选项
git clone https://github.com/xxxxx --config 'http.proxy=socks5://127.0.0.1:1234'
# 方法二、环境变量
ALL_PROXY='socks5://127.0.0.1:8888' git clone https://github.com/some/one.git
2.3. 特殊场景
仅针对某个地址使用网络加速(并且禁用 SSL 验证):
git config --global http.https://domain.com.proxy "http://proxy.example.com:port"
git config --global http.https://domain.com.sslVerify false
3. 相关链接
关于网络加速的更多配置,参考 Configure Git to use a proxy(https://gist.github.com/evantoli/f8c23a37eb3558ab8765%20) 页面。
4. 参考文献
Using a socks proxy with git for the http transport(https://stackoverflow.com/questions/15227130/using-a-socks-proxy-with-git-for-the-http-transport)