Git 代理配置
在国内直连 GitHub 经常出现
Connection was reset、Could not connect to server或超时。 本篇讲解如何让 Git 通过代理完成clone/pull/push。
1. 为什么需要代理
直连 GitHub 的典型报错:
fatal: unable to access 'https://github.com/xxx/yyy.git/':
Recv failure: Connection was reset
fatal: unable to access 'https://github.com/xxx/yyy.git/':
Failed to connect to github.com port 443 after 21058 ms: Could not connect to server
先确认本地代理端口是否可用(以 Clash 默认的 7890 为例):
Test-NetConnection 127.0.0.1 -Port 7890
返回 TcpTestSucceeded : True 即代理已就绪。
2. 给 Git 配置 HTTP/HTTPS 代理
假设代理地址为 127.0.0.1:7890,改成你自己的端口:
# 设置
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890
# 查看
git config --global --get http.proxy
# 取消(不需要代理时务必清除)
git config --global --unset http.proxy
git config --global --unset https.proxy
配置写入
~/.gitconfig,对所有仓库生效。
3. SOCKS5 代理
若代理提供的是 SOCKS5(如 Shadowsocks、v2rayN 的 socks 端口):
git config --global http.proxy socks5://127.0.0.1:1080
git config --global https.proxy socks5://127.0.0.1:1080
socks5://:由代理服务器解析域名socks5h://:由本地解析域名(DNS 也可能被污染时用这个)
git config --global http.proxy socks5h://127.0.0.1:1080
4. 只对 GitHub 走代理
不想影响公司内网等其他仓库时,可只针对 GitHub 域名配置:
git config --global http.https://github.com.proxy http://127.0.0.1:7890
取消:
git config --global --unset http.https://github.com.proxy
5. 环境变量方式
不写入配置文件,仅当前终端会话生效。
PowerShell
$env:HTTP_PROXY = "http://127.0.0.1:7890"
$env:HTTPS_PROXY = "http://127.0.0.1:7890"
CMD
set HTTP_PROXY=http://127.0.0.1:7890
set HTTPS_PROXY=http://127.0.0.1:7890
Bash / Git Bash
export HTTP_PROXY=http://127.0.0.1:7890
export HTTPS_PROXY=http://127.0.0.1:7890
环境变量优先级高于
git config,两者同时存在时以环境变量为准。
6. SSH 方式(git@github.com:...)
上面的 HTTP 代理对 SSH 协议无效,使用 SSH 克隆的仓库需配置 ~/.ssh/config:
Windows
Host github.com
HostName github.com
User git
ProxyCommand connect -H 127.0.0.1:7890 %h %p
connect.exe随 Git for Windows 一起安装,通常已在 PATH 中。
Linux / macOS
Host github.com
HostName github.com
User git
ProxyCommand nc -X 5 -x 127.0.0.1:7890 %h %p
测试:
ssh -T git@github.com
7. 常见代理软件端口对照
| 软件 | 常用端口 | 类型 |
|---|---|---|
| Clash / Clash Verge | 7890 | HTTP + SOCKS 混合 |
| v2rayN | 10809 | HTTP |
| v2rayN | 10808 | SOCKS5 |
| Shadowsocks | 1080 | SOCKS5 |
| 系统代理(Windows) | 设置 → 代理 中查看 | HTTP |
端口以你软件里的实际设置为准,不是固定值。
8. 排查与验证
# 1. 代理端口是否在监听
Test-NetConnection 127.0.0.1 -Port 7890
# 2. Git 当前代理配置
git config --global --get http.proxy
git config --global --list | Select-String proxy
# 3. 用代理测试访问
curl.exe -x http://127.0.0.1:7890 -I https://github.com
# 4. 克隆测试
git clone https://github.com/git/git.git --depth 1
仍失败时检查
- 代理软件是否开启、端口是否写错
- 是否同时设置了环境变量与
git config,二者冲突 - SSH 仓库需按第 6 节单独配置,不受 http.proxy 影响
- 切换网络后旧的代理配置可能失效,记得
--unset
9. 命令速查
# 设置 HTTP 代理
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890
# 设置 SOCKS5 代理
git config --global http.proxy socks5://127.0.0.1:1080
git config --global https.proxy socks5://127.0.0.1:1080
# 只对 GitHub
git config --global http.https://github.com.proxy http://127.0.0.1:7890
# 取消
git config --global --unset http.proxy
git config --global --unset https.proxy
# 查看
git config --global --get http.proxy