跳转到内容
Guides 3 分钟阅读 1346 次浏览

Git 代理配置

学习如何配置 Git 通过代理服务器访问网络。本指南涵盖 clone、push 和 pull 命令经由代理的配置方法。

Git 代理配置

Git 代理配置指的是设置 Git 的 http.proxyhttps.proxy 选项,或利用 HTTP_PROXYHTTPS_PROXY 等环境变量,或者在 SSH 配置中使用 ProxyCommand 处理基于 SSH 的 Git 操作,从而把流量通过一台中间服务器转发。

了解 Git 的代理需求

企业防火墙、网络安全策略或受限的互联网访问,常常要求把 Git 流量通过代理服务器转发。这适用于 git clonegit fetchgit pullgit push 等操作。具体配置方式取决于 Git URL 使用的协议:https://(或 http://)使用 HTTP/HTTPS 代理,而 ssh://(或 git@)使用 SSH 专属的代理方式。

HTTP/HTTPS Git 代理配置

对通过 https://http:// URL 访问的 Git 仓库,Git 会使用 HTTP/HTTPS 代理设置。

Git 配置方式(git config

这是为 Git 配置 HTTP/HTTPS 代理的推荐方式。配置可以是全局的(对所有仓库生效),也可以只针对某一个仓库。

全局配置

为系统上所有 Git 仓库全局配置代理:

# For HTTP proxy (e.g., for https://github.com URLs through an http proxy)
git config --global http.proxy http://proxy.example.com:8080

# For HTTPS proxy (e.g., for https://github.com URLs through an https proxy)
# Note: Often an HTTPS proxy is configured similarly to an HTTP proxy,
# but traffic is explicitly tunneled.
git config --global https.proxy http://proxy.example.com:8080

如果代理需要认证:

git config --global http.proxy http://username:[email protected]:8080
git config --global https.proxy http://username:[email protected]:8080

出于安全考虑,与其把密码直接写进 URL,不如让 Git 提示输入凭据,或从 credential helper 中获取。省略密码时 Git 会主动提示输入。

单仓库配置

要为特定仓库配置代理,进入该仓库的根目录并省略 --global 参数:

cd /path/to/my/repo
git config http.proxy http://proxy.example.com:8080
git config https.proxy http://proxy.example.com:8080

这些设置会保存在该仓库的 .git/config 文件中。

对特定主机禁用代理

要让某些域名(例如内部 Git 服务器)绕过代理,使用 http.noProxy

git config --global http.noProxy "localhost,127.0.0.1,*.internal.com"

多个主机可用逗号分隔。

SSL 证书校验

在使用透明代理或拦截式代理(MITM 代理)的企业环境中,代理会用自己的证书替代源服务器证书,Git 的 SSL 证书校验因此可能失败。

解决办法是让 Git 信任企业根 CA 证书:

git config --global http.sslCAInfo /path/to/corporate/ca-cert.pem

另一种做法是关闭 SSL 校验,但因安全风险,不建议在生产环境中使用

git config --global http.sslVerify false

这会跳过证书验证,使连接面临真实的中间人攻击风险。仅在临时排错或受控的隔离环境中使用。

环境变量

包括 curl 在内的命令行工具(Git 的 HTTP/HTTPS 传输常常依赖 curl)通常会遵循 HTTP_PROXYHTTPS_PROXYNO_PROXY 环境变量。这些变量提供系统级或会话级的代理设置,供应用程序继承。

# For HTTP proxy
export HTTP_PROXY="http://proxy.example.com:8080"
export HTTPS_PROXY="http://proxy.example.com:8080" # Often the same for HTTPS traffic tunneling

# For SOCKS proxy
export ALL_PROXY="socks5://proxy.example.com:1080"

# Exclude specific hosts from proxy
export NO_PROXY="localhost,127.0.0.1,*.internal.com"

请注意,对于 Git 自身的 HTTP/HTTPS 操作,http.proxyhttps.proxy 配置的优先级高于这些环境变量。当没有显式设置 git config 时,或者对于与 Git 交互的其他工具,环境变量就很有用。

对比:git config 与环境变量

特性 git config http.proxy / https.proxy HTTP_PROXY / HTTPS_PROXY 环境变量
作用范围 仅对 Git 生效(全局或单仓库) 对遵循这些变量的应用,系统级或会话级生效
优先级 在 Git 操作中更高;会覆盖环境变量。 对 Git 更低;作为兜底或供其他工具使用。
持久性 跨会话持久(保存在 .gitconfig.git/config)。 默认不持久;需写入 shell 配置文件(.bashrc.zshrc)才能持久。
认证 支持 URL 中的 username:password@,或提示输入凭据。 支持 URL 中的 username:password@
灵活性 可按 Git 仓库精细控制。 影响面更广,波及所有遵循这些变量的应用。

SSH Git 代理配置

对通过 ssh://git@ URL 访问的 Git 仓库,代理由 SSH 客户端配置管理,具体是 ~/.ssh/config 中的 ProxyCommand 指令。标准的 HTTP/HTTPS 代理设置(例如 http.proxy)不会影响 SSH 连接。

~/.ssh/config 中使用 ProxyCommand

ProxyCommand 告诉 SSH:连接目标主机(%h)和端口(%p)时,先经由一个外部命令转发,这个命令通常是某个代理客户端。

前置条件
  • netcatnc):用于基本的 TCP 隧道和 SOCKS 代理。
  • corkscrewconnect-proxy:用于把 SSH 通过需要认证的 HTTP/HTTPS 代理进行隧道传输。
HTTP/HTTPS 代理隧道

如果您的代理是 HTTP/HTTPS 代理,可以使用 corkscrewconnect-proxy

  1. 安装 corkscrewconnect-proxy(若尚未安装)。

    • Debian/Ubuntu:sudo apt-get install corkscrew
    • macOS(使用 Homebrew):brew install corkscrew
    • connect-proxy 通常随 openssh 一起提供,也可单独获取。
  2. 编辑 ~/.ssh/config
    创建或修改 ~/.ssh/config,内容如下:

    ```ssh
    Host github.com
    ProxyCommand corkscrew proxy.example.com 8080 %h %p
    # If proxy requires authentication:
    # ProxyCommand corkscrew proxy.example.com 8080 %h %p /path/to/proxy_auth_file

    Host gitlab.com
    ProxyCommand corkscrew proxy.example.com 8080 %h %p

    Or for all hosts

    Host *
    ProxyCommand corkscrew proxy.example.com 8080 %h %p
    `` 把proxy.example.com8080替换成您自己的代理地址和端口。/path/to/proxy_auth_file文件中应在单独一行写入username:password。请确保该文件权限足够严格(例如chmod 600`)。

SOCKS 代理隧道

如果您的代理是 SOCKS 代理,使用带 -X 选项的 netcatnc):

  1. 确认已安装 netcat 大多数类 Unix 系统通常已预装。

  2. 编辑 ~/.ssh/config

    ```ssh
    Host github.com
    ProxyCommand nc -X 5 -x proxy.example.com:1080 %h %p

    Host gitlab.com
    ProxyCommand nc -X 5 -x proxy.example.com:1080 %h %p

    Or for all hosts

    Host *
    ProxyCommand nc -X 5 -x proxy.example.com:1080 %h %p
    `` 把proxy.example.com1080替换成您自己的 SOCKS 代理地址和端口。 *-X 5:指定 SOCKS 版本 5。SOCKS 版本 4 请用-X 4。 *-x`:指定代理地址和端口。

SOCKS 代理的认证

部分 netcat 版本(Linux 上常见的默认版本,即 OpenBSD 的 nc)支持 SOCKS 代理认证。例如:

Host *
    ProxyCommand nc -X 5 -x user:[email protected]:1080 %h %p

具体的认证语法和功能请查阅您所用 netcat 版本的 man 手册。

验证代理配置

配置完代理后,验证 Git 操作是否正确走了代理。

针对 HTTP/HTTPS Git 操作

检查 Git 配置:

git config --global --get http.proxy
git config --global --get https.proxy
git config --global --get http.noProxy

对一个 HTTPS 仓库执行 git clonegit fetch

git clone https://github.com/git/git.git

如果代理正常,连接应当成功。网络监控工具可以进一步确认流量走向。

针对 SSH Git 操作

以详细模式测试 SSH 连接:

ssh -vT [email protected]

在输出中查找 ProxyCommand 被执行的信息。例如:

debug1: Executing proxy command: exec corkscrew proxy.example.com 8080 github.com 22

然后尝试一次 Git 操作:

git clone [email protected]:git/git.git

常见问题排查

  • 代理地址或端口错误: 仔细核对代理服务器的 IP 地址或主机名以及指定端口。
  • 认证失败: 核实代理的用户名和密码。使用 git config 时,确认凭据正确或已配置 credential helper;使用 ProxyCommand 时,确认认证文件或内嵌凭据正确。
  • SSL 证书问题(https://): 如果遇到 SSL certificate problem: self signed certificate in certificate chain 之类的报错,说明企业代理可能在做 SSL 检查。请用企业根 CA 证书配置 http.sslCAInfo;实在不行,可临时设置 http.sslVerify false 用于排错(不建议用于生产)。
  • 防火墙拦截: 确认本地防火墙(如果有)和企业防火墙允许在指定端口上向代理服务器发起出站连接。代理服务器本身也必须允许连接到 Git 托管服务。
  • 找不到 ProxyCommand 辅助程序(ssh://): 如果 ProxyCommand 报 "command not found",请确认已安装 netcatcorkscrewconnect-proxy,并且其可执行文件路径在系统的 PATH 环境变量中。必要时写出可执行文件的完整路径(例如 ProxyCommand /usr/bin/corkscrew ...)。
  • 代理被绕过: 如果 git confighttp.noProxy 范围过宽,或环境变量设置有误,Git 可能会尝试直连。请检查 noProxy 设置。
  • 代理超时: 若代理的超时设置过于激进,长时间运行的操作可能超时。这通常属于服务器端的配置问题。
已更新: 04.03.2026
返回分类

试用我们的代理

遍布 100+ 国家的 20,000+ 代理

support_agent
GProxy Support
Usually replies within minutes
Hi there!
Send us a message and we'll reply as soon as possible.