我喜欢做的是设置两个 Git 别名:
~/.gitconfig
[alias]
noproxy = config --global --remove-section http
proxy = config --global http.proxy http://127.0.0.1:9666
请注意,我没有使用config --global --unset http.proxy 重置代理,因为这会留下[http] 部分标题,因此在反复启用和禁用代理后,您的.gitconfig 将被一堆空的@987654325 污染@ 部分标题。没什么大不了的,只是很烦人。
在某些情况下,例如在公司防火墙后面,您需要改为配置 ~/.ssh/config。设置变得稍微复杂一些:
~/.gitconfig
[alias]
noproxy = !sh -c 'cp ~/.ssh/config.noproxy ~/.ssh/config'
proxy = !sh -c 'cp ~/.ssh/config.proxy ~/.ssh/config'
~/.ssh/config.noproxy
Host github.com-username
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
~/.ssh/config.proxy
Host *
ProxyCommand connect -H 127.0.0.1:9666 %h %p
Host github.com-username
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
您甚至可以通过将别名更改为以下方式来组合这两种方法:
[alias]
noproxy = !sh -c 'git config --global --remove-section http 2> /dev/null && cp ~/.ssh/config.noproxy ~/.ssh/config'
proxy = !sh -c 'git config --global http.proxy http://127.0.0.1:9666 && cp ~/.ssh/config.proxy ~/.ssh/config'
现在我可以简单地输入git noproxy 来禁用代理,并输入git proxy 来启用它。您甚至可以通过创建更多别名在多个代理之间切换。