对比

方式 位置 只代理GitHub代码 git clone gh效果
ssh ~/.ssh/config配置 Host github.com
HostName github.com
User git
ProxyCommand nc -v -x 127.0.0.1:7890 %h %p
飞快
https 终端输入 git config --global http.https://github.com.proxy socks5://127.0.0.1:7890 飞快

使用理由

1
2
git clone --depth=1 git@github.com:jerryc127/hexo-theme-butterfly.git
git clone --depth=1 https://github.com/jerryc127/hexo-theme-butterfly.git

浅拷贝git clone --depth=1 ref速度确实快,但是版本回退是问题,在 GitHubPull requests非常不方便,而ssh全拷贝会方便进行git的操作,可是全拷贝速度奇慢,只能通过代理下载,下文有介绍https

依赖

1
2
3
4
代理 # telnet 127.0.0.1 7890
macOS # 测试环境是macOS 11.1
git --version
ssh -T git@github.com

SSH

设置macOS代理

修改 ~/.ssh/config 文件(不存在则新建rm -rf ~/.ssh/config && touch ~/.ssh/config):

1
2
3
4
5
6
7
8
# 必须是 github.com
Host github.com
HostName github.com
User git
# 走 HTTP 代理
# ProxyCommand socat - PROXY:127.0.0.1:%h:%p,proxyport=8080
# 走 socks5 代理(如 Shadowsocks)
ProxyCommand nc -v -x 127.0.0.1:7890 %h %p

从 GitHub 克隆代码

1
git clone git@github.com:jerryc127/hexo-theme-butterfly.git

Https

删代理

1
2
3
4
git config --unset http.proxy # 系统 > 全局 > 当前 
git config --local --unset http.proxy # 当前
git config --global --unset http.proxy # 全局
git config --system --unset http.proxy # 系统

查全局所有代理

1
2
git config --global --list
# git config --system --list # 系统,一般用不到

proxy结果

1
2
https.https://github.com.proxy=https://127.0.0.1:7890
http.https://github.com.proxy=socks5://127.0.0.1:7890

删除proxy

1
2
git config --global --unset http.https://github.com.proxy
git config --global --unset https.https://github.com.proxy

全局代理

我们使用git config http.proxy socks5://127.0.0.1:7890只能当前仓库,适用于git pushgit pull等。由于git clone的是其他仓库,所以需要使用全局或系统设置,我选全局代理。

1
2
git config --global http.https://github.com.proxy socks5://127.0.0.1:7890 # 只代理GitHub的git
# git config --global http.proxy socks5://127.0.0.1:7890 # 代理任何git,包括国内gitee

查询代理设置

1
git config http.proxy

显示socks5://127.0.0.1:7890即可

从 GitHub 克隆代码

1
git clone https://github.com/jerryc127/hexo-theme-butterfly.git # https

参考

git官方文档:https://git-scm.com/doc

macOS设置git的ssh代理主要参考:https://gist.github.com/chuyik/02d0d37a49edc162546441092efae6a1

https://gist.github.com/chenshengzhi/07e5177b1d97587d5ca0acc0487ad677

https://blog.csdn.net/u013247461/article/details/100164454?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromBaidu-1.control&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromBaidu-1.control

https://blog.csdn.net/default7/article/details/100068256