作为开发者,我们常遇到企业内网或特殊网络环境下无法直接访问Git仓库的情况。本教程将详细介绍如何为Git设置代理(Proxy)以及如何取消代理配置,并提供常见问题排查方法。
以下场景需要使用Git代理:
bash# 设置HTTP协议代理
git config --global http.proxy http://proxy.example.com:8080
# 设置HTTPS协议代理
git config --global https.proxy https://proxy.example.com:8080
# 带认证信息的代理(用户名/密码)
git config --global http.proxy http://user:[email protected]:8080
验证配置生效:
bashgit config --global --get http.proxy git config --global --get https.proxy
进入项目目录后执行:
bashgit config http.proxy http://proxy.example.com:8080
协议类型 | 适用场景 | 示例格式 |
---|---|---|
HTTP | 普通HTTP仓库 | http://proxy.com:8080 |
HTTPS | HTTPS仓库/增强安全性 | https://proxy.com:8080 |
SOCKS5 | 需要加密传输 | socks5://proxy.com:1080 |
bash# 全局取消
git config --global --unset http.proxy
git config --global --unset https.proxy
# 项目级取消(需进入项目目录)
git config --unset http.proxy
打开全局配置文件:
bash# Windows
notepad %USERPROFILE%\.gitconfig
# Linux/macOS
nano ~/.gitconfig
删除包含以下内容的行:
ini[http]
proxy = http://proxy.example.com:8080
[https]
proxy = https://proxy.example.com:8080
对特定域名禁用代理:
ini[http "https://github.com/"]
proxy = ""
单次命令禁用代理:
bashgit -c http.proxy= clone https://github.com/example/repo.git
编辑SSH配置文件 ~/.ssh/config
:
Host github.com Hostname github.com User git ProxyCommand nc -x 127.0.0.1:1080 %h %p
bashcurl -v -x http://proxy.example.com:8080 https://github.com
错误提示:
fatal: unable to access 'https://github.com/.../': Failed to connect to proxy...
解决步骤:
git config --list
检查配置冲突Git的代理设置存储在以下位置:
~/.gitconfig
(Linux/macOS)或 %USERPROFILE%\.gitconfig
(Windows).git/config
配置优先级(从高到低):
本文作者:Dong
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC。本作品采用《知识共享署名-非商业性使用 4.0 国际许可协议》进行许可。您可以在非商业用途下自由转载和修改,但必须注明出处并提供原作者链接。 许可协议。转载请注明出处!