跳到正文

Git 怎么设置代理:http.proxy、SOCKS5 和 SSH 仓库

让 Git 走代理的做法:HTTPS 仓库一条命令;只给某一个站点设置不同的代理;SOCKS5;怎么再关掉;SSH 仓库;以及怎么确认真的生效。

代理改变什么,不改变什么

代理只改变你的电脑到 Git 服务器之间的网络路径,不改变你所用的 Git 托管服务的规则,也不改变谁可以在哪里使用它。Git 本身可以配合任何只负责转发流量的普通 HTTP 或 SOCKS5 代理使用。

两种仓库地址,要两种设置

「远程地址」就是 Git 要连接的地址,运行 git remote -v 就能看到。如果以 https://(或 http://)开头,Git 通过 curl 库联网,http.proxy 设置有效;如果以 git@ 或 ssh:// 开头,Git 把连接交给 ssh,http.proxy 会被忽略,这种情况看下面的 SSH 一节。分不清的话,最省事的办法是把远程地址改成 https:// 形式再用代理。

给所有 HTTPS 仓库设一个代理

git-config 文档把 http.proxy 描述为对环境变量 http_proxy、https_proxy、all_proxy 所设代理的覆盖。你只需要这一个键,https:// 地址也由它负责。取值格式是 [协议://][用户名[:密码]@]主机[:端口]。示例里的第二条命令会把值打印出来,是确认当前设置最快的办法。

给所有 HTTPS 仓库设一个代理:示例

git config --global http.proxy http://USERNAME:PASSWORD@HOST:PORT
git config --global --get http.proxy

请把 HOST、PORT、USERNAME 和 PASSWORD 换成「我的资产」里的值。

账号和密码

账号或密码里如果有特殊字符,要在地址里做百分号编码:@ 写成 %40,: 写成 %3A,/ 写成 %2F,# 写成 %23,% 写成 %25。从「我的资产」复制比手打更不容易出错。git-config 文档也允许只写用户名不写密码,比如 http://USERNAME@HOST:PORT,这时 Git 会像处理其他凭据那样去问你要密码,密码就不会存在文件里。注意 git config --global 会把内容以明文写进 ~/.gitconfig,地址里带密码的话,能读这个文件的人都能看到。

只给某个站点设不同的代理,或者不用代理

任何 http.* 选项都可以只对某些网址生效,做法是把网址写进键名:http.<网址>.proxy。协议和主机必须和远程地址完全一致,更长的匹配优先。比如只有 GitHub 需要走代理、公司内部的 Git 服务器要直连时就很方便。把 remote.<名称>.proxy 设成空字符串,可以只对那一个远程关闭代理,git-config 文档写明了这一点;文档没说 http.<网址>.proxy 设成空是什么效果,所以只想关掉单个远程的代理时,请用 remote.<名称>.proxy 的写法。

只给某个站点设不同的代理:示例

git config --global http.https://github.com/.proxy http://USERNAME:PASSWORD@HOST:PORT
git config remote.origin.proxy ""

第一行只对 github.com 设置代理。第二行在当前仓库里关闭名为 origin 的远程的代理。

只对一条命令试用

想先试试代理又不想保存任何设置,可以用 git -c 只对这一次运行传入设置。~/.gitconfig 不会被改动,密码也不会留在配置文件里(命令运行期间在命令行里仍然看得到)。

只对一条命令试用:示例

git -c http.proxy=http://USERNAME:PASSWORD@HOST:PORT clone https://github.com/git/git.git

请把 HOST、PORT、USERNAME 和 PASSWORD 换成「我的资产」里的值。

SOCKS5 和 socks5h

Git 会把代理字符串交给 curl,Git 文档也指向 curl 能理解的写法。curl 文档列出了 socks5:// 和 socks5h://;用 socks5h:// 时由代理而不是你的电脑去解析网站域名。没有特别理由就用 socks5h://。Git 的页面本身没有列出这些协议前缀,所以请用下面的检查方法在你的 Git 版本上确认它能用。

SOCKS5 和 socks5h:示例

git config --global http.proxy socks5h://USERNAME:PASSWORD@HOST:PORT

请把 HOST、PORT、USERNAME 和 PASSWORD 换成「我的资产」里的值。

怎么关掉代理

用 --unset 删除这个键。如果同一个键设在多个地方,用 --unset-all,再运行 git config --global --list 确认已经没有 http.proxy 这一行。如 git-config 文档所说,环境变量 http_proxy、https_proxy、all_proxy 也会被读取,如果代理总是自己回来,也要在终端里把这些变量取消。

怎么关掉代理:示例

git config --global --unset http.proxy
git config --global --unset-all http.https://github.com/.proxy

之后运行 git config --global --list 看看还剩什么。

SSH 仓库(git@... 和 ssh://...)

http.proxy 对 SSH 仓库不起作用,因为 Git 实际是运行 ssh,由 ssh 去建立连接。在 ssh 里要设 ProxyCommand:它是一条替 ssh 打开到服务器连接的命令,其中 %h 代表远程主机,%p 代表远程端口,ssh_config 手册里有说明。手册自己的例子是用 nc 的 -X connect 连 HTTP 代理。ProxyJump 不能代替它:它只用于经由另一台 SSH 主机跳转,而不是经由 SOCKS 或 HTTP 代理。

带账号密码的代理需要一个能发送它们的工具。OpenBSD nc 的手册写明代理认证目前只支持 HTTP CONNECT 代理,而且只有用户名选项、没有密码选项,所以不适合带账号密码的 SOCKS5 代理。Nmap 项目的 Ncat 工具有 --proxy-type socks5 和 --proxy-auth USERNAME:PASSWORD,正好用于这个场景。请先安装 Ncat(它随 Nmap 一起提供)。下面第一个示例是 ssh_config 的写法;第二个示例通过 GIT_SSH_COMMAND 只对这一次运行传入同样的 ProxyCommand,Git 文档说明这个变量由 shell 解释执行。

Ncat 的页面没有演示把它放进 ProxyCommand,所以请用下面的检查方法自己测试。另外要知道,有些代理只放行网页端口,会拒绝连到 22 端口;如果连接立刻被关闭,请把远程地址改成 https://。

SSH 仓库:ssh_config 示例

# ~/.ssh/config
Host github.com
  ProxyCommand ncat --proxy HOST:PORT --proxy-type socks5 --proxy-auth USERNAME:PASSWORD %h %p

请把 HOST、PORT、USERNAME 和 PASSWORD 换成「我的资产」里的值。

SSH 仓库:用 GIT_SSH_COMMAND 只运行一次

GIT_SSH_COMMAND='ssh -o "ProxyCommand=ncat --proxy HOST:PORT --proxy-type socks5 --proxy-auth USERNAME:PASSWORD %h %p"' git ls-remote [email protected]:git/git.git HEAD

请把 HOST、PORT、USERNAME 和 PASSWORD 换成「我的资产」里的值。

怎么确认 Git 在用代理

  1. 先打印当前设置

    运行 git config --show-origin --get-all http.proxy。它会显示取值以及来自哪个文件,方便找出被遗忘的设置。

  2. 打开跟踪运行一条小命令

    git ls-remote 只向服务器要分支列表,不下载文件。加上 GIT_TRACE_CURL=1,开头几行会显示 Git 连接的主机和端口:应该是你的代理,而不是 Git 服务器。

  3. 用 curl 看出口地址

    下面示例里的第二条命令会显示网站通过同一个代理看到的 IP、国家和网络。各字段的含义见「如何查看代理的 IP、国家和运营商」指南。

  4. 最后真的克隆一次

    设好代理后,克隆一个你有权读取的小仓库。能成功,而且跟踪显示连的是代理主机,就完成了。

怎么确认 Git 在用代理:示例

GIT_TRACE_CURL=1 git ls-remote https://github.com/git/git.git HEAD
curl -x http://USERNAME:PASSWORD@HOST:PORT https://ipinfo.io/json

请把 HOST、PORT、USERNAME 和 PASSWORD 换成「我的资产」里的值。

常见错误

• Proxy CONNECT aborted 或 407 Proxy Authentication Required。代理拒绝了账号或密码。重新复制一遍,并检查百分号编码。

• Could not resolve proxy 或 Failed to connect。HOST 或 PORT 不对,或者租期已结束。用上面的 curl 命令测试同一个地址。

• curl 能走代理但 Git 卡住。用 git config --show-origin --get-all http.proxy 看有没有遗留的值。

• 用 ssh 克隆时代理不起作用。这是正常的:按上面的写法设置 ProxyCommand,或改用 https:// 地址。

• ssh 连接立刻被关闭。代理可能不放行 22 端口,请改用 https:// 远程地址。

用 Proxy302 怎么做

这里的每条命令都只需要 Proxy302 代理的主机、端口、账号和密码。动态住宅流量按 GB 计费,出口国家在生成代理时设置;HTTP 在 proxy.proxy302.com 的 2222 端口,SOCKS5 在 3333 端口。静态 ISP IP 在整个租期内固定、不轮换,适合希望每次都从同一个地址出去的工具。HTTP 还是 SOCKS5 在购买时选择,一个 IP 只带一种协议。

用 Proxy302 怎么做:步骤

  1. 第 1 步

    打开动态住宅页面,选择线路,「协议」保持 HTTP(或改选 SOCKS5),然后点击「确认费用并生成」。

  2. 第 2 步

    打开「我的资产」(控制台里放着你买到的全部代理),复制 HOST(地址)、PORT(端口)、USERNAME(账号)和 PASSWORD(密码)。「连接工具」页里有填好你自己连接信息的现成示例。

  3. 第 3 步

    把这些值填进上面的命令。

  4. 第 4 步

    通过代理访问 https://ipinfo.io/json 确认出口。

  5. 第 5 步

    在中国大陆,先在「连接工具」页打开辅助工具,使用代理期间保持开着。

价格与限制

低价线路每 GB 收费 US$1.50,100 GB 套餐每 GB US$1.35,300 GB 每 GB US$1.20。静态 ISP IP 一天 US$0.50,30 天 US$7.99。不提供免费试用,最小购买单位为 1 GB,或一个 IP 一天。可用的出口位置每天都会变化。

常见问题

有 git config https.proxy 吗?

git-config 文档记载的是 http.proxy,它覆盖环境变量 http_proxy、https_proxy 和 all_proxy 所设的代理,https:// 仓库也用这个键。

为什么用 [email protected] 克隆时代理不起作用?

SSH 仓库是由 ssh 处理的,不经过 curl。请在 ~/.ssh/config 里设置 ProxyCommand,或把远程地址改成 https://。

Git 该选 HTTP 还是 SOCKS5?

https:// 仓库用 HTTP 最简单。如果你买的是 SOCKS5,或者要用带账号密码的 ProxyCommand 走 ssh,就选 SOCKS5 并写 socks5h://。

怎么只对某一个仓库使用代理?

在那个仓库目录里运行不带 --global 的 git config 命令:设置会写进该仓库自己的 .git/config,而不是 ~/.gitconfig。

参考资料

7
  1. Git docs: git-config (http.proxy, http.<url>.*, remote.<name>.proxy)

    官方文档 · 确认日期:

    Read on 2 November 2026: http.proxy overrides the proxy normally set by the http_proxy, https_proxy and all_proxy variables; it accepts the syntax curl understands plus a user name without a password (Git then asks for the password the way it does for other credentials); the stated syntax is [protocol://][user[:password]@]proxyhost[:port][/path]; remote.<name>.proxy overrides it per remote and an empty string disables proxying for that remote; http.<url>.* options apply to matching URLs, the scheme and host must match.

    The page does not list the proxy schemes (socks5, socks5h) and does not say what an empty http.proxy does. The socks5h scheme comes from the curl documentation below: test it.

  2. Git docs: git (environment variables GIT_TRACE_CURL, GIT_SSH_COMMAND)

    官方文档 · 确认日期:

    Read on 2 November 2026: GIT_TRACE_CURL enables a curl full trace dump of the transport, similar to curl --trace-ascii; GIT_SSH_COMMAND is used instead of ssh for fetch and push, takes precedence over GIT_SSH and is interpreted by the shell.

    The first part of the page (about 100,000 characters) was read; GIT_CURL_VERBOSE was not found there, so this guide uses GIT_TRACE_CURL.

  3. libcurl docs: CURLOPT_PROXY

    官方文档 · 确认日期:

    Read on 2 November 2026: socks5:// is a SOCKS5 proxy; socks5h:// is a SOCKS5 proxy where the proxy resolves the URL hostname; an empty string explicitly disables the proxy even if an environment variable is set; all_proxy is used if no protocol-specific proxy was set.

    Git passes the proxy string to curl; the Git documentation itself only points to curl's syntax.

  4. OpenBSD manual: ssh_config (ProxyCommand, ProxyJump)

    官方文档 · 确认日期:

    Read on 2 November 2026: ProxyCommand specifies the command used to connect to the server and accepts the tokens %h (remote host), %p (remote port), %r and %n; the manual's example is ProxyCommand /usr/bin/nc -X connect -x 192.0.2.0:8080 %h %p; ProxyJump takes one or more [user@]host[:port] jump hosts and makes an ssh connection through them.

    ProxyJump is described only for ssh hops; the page does not say it can use a SOCKS or HTTP proxy.

  5. OpenBSD manual: nc

    官方文档 · 确认日期:

    Read on 2 November 2026: -X selects the proxy protocol (4, 4A, 5, connect), -x gives the proxy address and port, -P gives a proxy user name, and the manual states that proxy authentication is only supported for HTTP CONNECT proxies at present; no password option is described.

    Other nc builds can differ; this describes the OpenBSD one.

  6. Nmap Ncat guide: proxies

    官方文档 · 确认日期:

    Read on 2 November 2026: ncat --proxy <host>[:<port>] --proxy-type {http|socks4|socks5} <host> [<port>], and --proxy-auth <username>:<password> for HTTP and SOCKS5 proxies.

    The page does not show Ncat inside an SSH ProxyCommand; that use follows from the ssh_config manual and is untested here.

  7. Proxy302 product facts and price list

    产品观察 · 确认日期:

    Product facts file of the site (values checked against the console, the backend and the price table on 18-19 October 2026): products, prices in US$ (1 PTC = US$1), lease terms, protocols and ports, countries, quantity limits, no free trial.

准备好试试了吗?

注册账号、充值一次,就能选动态住宅、静态 ISP 或数据中心 IP,几分钟配进你的浏览器。