使用 Git Clone 时添加代理的方法
在使用 git clone 命令时,如果需要通过代理服务器访问网络,可以按照以下方式设置代理:
1. 命令行直接添加代理参数
git clone -c http.proxy=http://your-proxy-server:port <repository-url>例如:
git clone -c http.proxy=http://127.0.0.1:1080 https://github.com/user/repo.git2. 全局 Git 配置代理
设置全局代理
git config --global http.proxy http://your-proxy-server:port
git config --global https.proxy http://your-proxy-server:port取消全局代理
git config --global --unset http.proxy
git config --global --unset https.proxy3. 针对特定域名设置代理
git config --global http.https://github.com.proxy http://your-proxy-server:port
git config --global https.https://github.com.proxy http://your-proxy-server:port4. 使用环境变量方式
export HTTP_PROXY=http://your-proxy-server:port
export HTTPS_PROXY=http://your-proxy-server:port
git clone <repository-url>5. 认证代理(需要用户名和密码)
git config --global http.proxy http://username:password@your-proxy-server:port注意事项
代理协议选择:根据代理服务器支持的协议选择 http:// 或 socks5://常见SOCKS5代理格式:socks5://127.0.0.1:1080
临时使用代理:推荐在clone时临时指定代理,而不是设置全局代理临时方法不会影响其他Git操作
代理性能测试:
git clone -c http.proxy=http://your-proxy:port --progress <repo-url>使用 --progress 参数可以查看克隆进度,测试代理效果
取消代理缓存:如果遇到证书问题,可能需要清除缓存:
git config --global --unset http.sslVerify git config --global http.sslVerify false请根据实际网络环境和代理服务器配置调整上述命令中的代理地址、端口、用户名和密码。如果不需要代理后,建议使用 --unset 清除配置。

