git config --global http.proxy
及
https.proxy
指定代理地址和端口,需替换协议、IP和端口,完成后用
--unset
取消,支持HTTP/HTTPS/SOCKS5协议,SSH代理需修改
~/.ssh/config
文件。
适用于通过http://
或https://
协议克隆的仓库
# 设置全局代理(所有仓库生效) git config --global http.proxy http://127.0.0.1:1080 git config --global https.proxy http://127.0.0.1:1080 # 设置特定域名代理(示例:GitHub) git config --global http.https://github.com.proxy socks5://127.0.0.1:1080
参数说明:
http://
:根据实际代理类型选择(http/https/socks5)0.0.1:1080
:替换为实际代理服务器地址和端口socks5://
前缀时需Git版本≥2.34适用于通过git@github.com:
形式克隆的仓库
编辑SSH配置文件
vim ~/.ssh/config
添加代理配置(示例)
Host github.com User git ProxyCommand connect -S 127.0.0.1:1080 %h %p
注意:
connect
工具(macOS: brew install connect
)-S
表示使用SOCKS5代理,HTTP代理使用-H
# 查看所有配置 git config --global --list # 测试网络连接 git ls-remote https://github.com/git/git.git
# 移除HTTP/HTTPS代理 git config --global --unset http.proxy git config --global --unset https.proxy # 移除SSH代理配置 编辑~/.ssh/config删除对应内容
证书错误
添加环境变量跳过SSL验证(临时方案):
export GIT_SSL_NO_VERIFY=1
密码认证代理
在代理地址中添加认证信息:
git config --global http.proxy http://user:password@proxy.server.com:8080
多平台适配
Windows用户建议使用Git Bash执行命令,路径使用替代
本文配置方法已在Git 2.34+版本测试验证,适用于Windows/macOS/Linux系统,建议优先使用HTTPS代理,SSH代理需要额外工具支持,网络设置可能受企业防火墙策略影响,如遇复杂网络环境请联系IT管理员获取专业支持。
引用说明:本文参考Git官方文档及Stack Overflow权威解决方案,配置命令均经过技术验证。