241 字
1 分钟
Git 使用 HTTP/HTTPS 代理访问 GitHub

在某些网络环境下,通过 HTTP/HTTPS 协议访问 GitHub 可能需要使用代理 Git 提供了配置方式,既可以仅针对特定域名(如 g.com)生效,也可以对所有 HTTPS 请求全局生效

一、设置代理#

1.1 仅为 GitHub 的 HTTPS 请求设置代理#

Terminal window
git config --global http.https://github.com/.proxy http://127.0.0.1:7890

对应的配置会写入 ~/.gitconfig 文件:

[http "https://github.com/"]
proxy = http://127.0.0.1:7890

1.2 为所有 HTTPS 请求设置代理#

Terminal window
git config --global https.proxy http://127.0.0.1:7890

二、取消代理#

2.1 取消 GitHub 专用代理#

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

2.2 取消全局代理#

Terminal window
git config --global --unset https.proxy

三、带账号密码的 HTTP/HTTPS 代理#

如果代理服务器需要认证,可以在代理地址中直接写入用户名和密码
(此处为代理服务器的认证信息,并非 GitHub 账号):

Terminal window
git config --global http.https://github.com/.proxy http://username:password@127.0.0.1:7890
NOTE
  • 如果用户名或密码中包含特殊字符(如 @:#),需要进行 URL 编码:
    • @%40
    • :%3A
    • #%23
  • 例如密码 pa@ss:word 应写成:
    http://user:pa%40ss%3Aword@127.0.0.1:7890
Git 使用 HTTP/HTTPS 代理访问 GitHub
https://www.self4m.com/posts/git-http-https-proxy-github/
✍️作者
Self4m
📅发布于
2025-09-15
©️许可协议
CC BY-NC-SA 4.0

商业用途必须事先获得作者授权;
非商业用途可以使用,但必须注明出处;
若有改编需采用相同许可协议发布。