最近开始刷 MIT 6.S081,git clone实在是龟速,上网查了为各个协议设置代理的方法,在此记录。
前提
请确认本地已经打开代理,切换到「全局模式」。找到监听的端口,比如我使用的客户端是 ClashX,可以点击Copy shell command来确认端口:
# 得到的结果
export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:7891# 得到的结果
export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:7891最简单的解决方案
使用 proxychains(现在新版本是叫proxychains-ng)一站式解决所有协议的配置。
$ brew install proxychains-ng
$ proxychains4 git clone xxx$ brew install proxychains-ng
$ proxychains4 git clone xxxhttp://
为防止 clone 国内仓库速度慢,我们只为 GitHub 设置代理:
$ git config --global http.http://github.com.proxy https://127.0.0.1:7890
$ git config --global http.https://github.com.proxy https://127.0.0.1:7890
# 如果之后 clone 报错可以加上以下两行
$ git config --global http.sslverify false
$ git config --global remote.origin.proxy 127.0.0.1:7890$ git config --global http.http://github.com.proxy https://127.0.0.1:7890
$ git config --global http.https://github.com.proxy https://127.0.0.1:7890
# 如果之后 clone 报错可以加上以下两行
$ git config --global http.sslverify false
$ git config --global remote.origin.proxy 127.0.0.1:7890取消设置:
$ git config --global --unset http.proxy
$ git config --global --unset https.proxy$ git config --global --unset http.proxy
$ git config --global --unset https.proxyssh:// (git@)
这需要修改~/.ssh/config(如果没有则新建)。将以下代码加入config文件,填入你的ip和port:
Host github.com
HostName github.com
User git
ProxyCommand nc -v -x <your_proxy_host>:<your_proxy_host> %h %pHost github.com
HostName github.com
User git
ProxyCommand nc -v -x <your_proxy_host>:<your_proxy_host> %h %pgit://
对于 GitHub ,可以利用insteadOf 来快速实现:
git config --global url.https://github.com/.insteadOf git://github.com/git config --global url.https://github.com/.insteadOf git://github.com/但是,对于 MIT 给出的仓库地址,git://g.csail.mit.edu/xv6-labs-2020,这个是行不通的。所以我们需要修改core.gitProxy。先写一个git-proxy的脚本,并把它放到PATH能找到的地方,再进行修改。
$ echo "nc -x <your_proxy_host>:<your_proxy_host> $1 $2" > /usr/local/bin/git-proxy
$ git config --global --add core.gitProxy git-proxy$ echo "nc -x <your_proxy_host>:<your_proxy_host> $1 $2" > /usr/local/bin/git-proxy
$ git config --global --add core.gitProxy git-proxy大功告成!