分辨需要设置的代理
HTTP 形式:
git clone https://github.com/owner/git.git
SSH 形式:
git clone git@github.com:owner/git.git
Git支持四种协议1,而除本地传输外,还有:git://, ssh://, 基于HTTP协议,这些协议又被分为哑协议(HTTP协议)和智能传输协议。对于这些协议,要使用代理的设置也有些差异:
- 使用
git协议
时,设置代理需要配置core.gitproxy
- 使用
HTTP协议
时,设置代理需要配置http.proxy
- 而是用
ssh协议
时,代理需要配置ssh的ProxyCommand
参数
1.Http形式 代理设置
走 HTTP 代理
git config --global http.proxy "http://127.0.0.1:8080"
git config --global https.proxy "http://127.0.0.1:8080"
走 socks5 代理(如 Shadowsocks)
git config --global http.proxy "socks5://127.0.0.1:1080"
git config --global https.proxy "socks5://127.0.0.1:1080"
取消设置
git config --global --unset http.proxy
git config --global --unset https.proxy
- 命令:
git config --global http.proxy http://127.0.0.1:1087
- 说明: 全局设置 http 代理
- 此命令修改的文件为
C:\Users\用户名\.gitconfig
(Windows 环境下) - 取消设置
git config --global --unset http.proxy
2. SSH 形式代理设置
2.1 设置本地 http 或 socks代理(Windows)
修改的文件为 C:\Users\用户名\.ssh\config (不存在自行创建)
配置方案1:
Host github.com *.github.com
User git
# SSH默认端口22, HTTPS默认端口443
Port 22
Hostname %h
# 这里放你的SSH私钥
IdentityFile ~\.ssh\id_rsa
# 设置代理, 127.0.0.1:1080 换成你自己代理软件监听的本地地址
# HTTPS使用-H,SOCKS使用-S
ProxyCommand connect -S 127.0.0.1:1080 %h %p
配置方案2:
# 这里的 -a none 是 NO-AUTH 模式,参见 https://bitbucket.org/gotoh/connect/wiki/Home 中的 More detail 一节
ProxyCommand connect -S 127.0.0.1:1080 -a none %h %p
Host github.com
User git
Port 22
Hostname github.com
# 注意修改路径为你的路径
IdentityFile "C:\Users\用户名\.ssh\id_rsa"
TCPKeepAlive yes
Host ssh.github.com
User git
Port 443
Hostname ssh.github.com
# 注意修改路径为你的路径
IdentityFile "C:\Users\用户名\.ssh\id_rsa"
TCPKeepAlive yes
说明:connect.exe
已经在 Git 中预置了,无需再次下载安装。connect 具体命令参数参考官方说明文档
该方式的配置中,如果 Host 设置为 *,那么 Host * 对应的配置会被应用到所有没有独立配置 的 ssh 连接中,包括使用了 ssh 协议的 git 操作。
2.2 其他设置方法(Linux\MacOS)
# 必须是 github.com
Host github.com
HostName github.com
User git
# 走 HTTP 代理
# ProxyCommand socat - PROXY:127.0.0.1:%h:%p,proxyport=8080
# 走 socks5 代理
# ProxyCommand nc -v -x 127.0.0.1:1080 %h %p
此方法走 socks代理设置有效,http 代理验证会报错
参考文章地址
https://upupming.site/2019/05/09/git-ssh-socks-proxy/
https://bitbucket.org/gotoh/connect/wiki/Home
https://gist.github.com/chuyik/02d0d37a49edc162546441092efae6a1
https://www.hi-linux.com/posts/11850.html
https://blog.systemctl.top/2017/2017-09-28_set-proxy-for-git-and-ssh-with-socks5/
https://gist.github.com/chuyik/02d0d37a49edc162546441092efae6a1