git设置socks5代理最直接有效的方式是执行git config --global http.proxy "socks5://127.0.0.1:10808"和git config --global https.proxy "socks5://127.0.0.1:10808",二者必须同时配置,且协议必须为socks5://(非socks5h://),地址端口间无空格,否则https仓库将直连失败。

git config --global http.proxy 和 git config --global https.proxy 是配置 SOCKS5 代理最直接有效的两个命令,不需要改文件、不依赖 GUI 工具,命令执行完立刻生效。
git config --global 配置 SOCKS5 代理的正确写法
SOCKS5 代理比 HTTP 代理更稳定,尤其对 GitHub、GitLab 等 HTTPS 地址兼容性更好。但写错格式会直接导致 git clone 报错或静默失败。
- 必须用
socks5://(不是socks5h://,Git 不识别后者) - 地址和端口之间不能有空格,冒号后紧跟端口号
- 若本地代理运行在
127.0.0.1:10808,命令是:
git config --global http.proxy "socks5://127.0.0.1:10808" git config --global https.proxy "socks5://127.0.0.1:10808"
- 如果你用的是 Clash、Proxyman 或 Surge,端口可能是
7891或1080,按实际填 - 不要混用协议:HTTP 代理用
http://,SOCKS5 必须统一用socks5://
常见错误现象:
-
fatal: unable to access '@#@#@#@#@#@#@#@#@#@0': Failed to connect to github.com port 443: Connection refused→ 代理地址/端口错,或代理进程没开 -
fatal: unable to access '@#@#@#@#@#@#@#@#@#@0': Unknown protocol→ 写成了socks5h://或漏了://
为什么 https.proxy 不能省略?
Git 对 https:// 地址(比如 @#@#@#@#@#@#@#@#@#@2)默认走 https.proxy,而不是复用 http.proxy。即使你只配了 http.proxy,HTTPS 请求仍会直连,导致超时或被墙。
-
https://仓库地址 → 查https.proxy -
http://仓库地址 → 查http.proxy - 大多数现代 Git 仓库都用 HTTPS 协议,所以两个都得设
验证是否生效:
git config --global --get http.proxy git config --global --get https.proxy
输出应为 socks5://127.0.0.1:10808 这类字符串,不是空或 http:// 开头。
对比基线与当前 GitHub Actions 运行导出,在 CI 成本和交付周期激增前及时发现工作流或作业运行时性能退化。
按域名单独配代理(避免影响内网 Git)
全局代理有时会干扰公司内网 Git 服务器(比如 @#@#@#@#@#@#@#@#@#@3),这时可以给特定域名跳过代理:
git config --global http.https://github.com.proxy "socks5://127.0.0.1:10808" git config --global http.https://gitlab.com.proxy "socks5://127.0.0.1:10808"
注意:
- 键名是
http.@#@#@#@#@#@#@#@#@#@4,不是https.@#@#@#@#@#@#@#@#@#@4 - 这种配置优先级高于全局
https.proxy - 内网地址如
git.internal.company不配,就走直连
取消某条域名代理:
git config --global --unset http.https://github.com.proxy
取消代理和排查残留问题
取消代理不能只删一行命令,必须成对清除:
git config --global --unset http.proxy git config --global --unset https.proxy
容易被忽略的点:
- TortoiseGit、SourceTree 等 GUI 工具读的是同一份
.gitconfig,改完命令行后不用重启工具 - 某些代理客户端(如 Clash for Windows)会自动修改系统代理,但 Git 不走系统代理,只认自己的配置
- 如果你在项目目录下执行过
git config --local,该配置会覆盖全局,需用--local --unset单独清理
最后检查:git ls-remote -h @#@#@#@#@#@#@#@#@#@6 能快速验证连通性。卡住超过 10 秒,基本就是代理没生效或端口不通。










