Git 代理配置完全指南

这篇文章梳理 Git 中所有代理配置方式,从简单的 git config 到 SSH 协议的 SOCKS5 隧道,一份指南全搞定。 为什么需要代理 访问 GitHub、GitLab 等平台时,有时会遇到 git clone 速度极慢甚至超时的情况。尤其是在国内网络环境下,给 Git 配置代理几乎是必备技能。 flowchart LR A[你的电脑] --> B{需要代理?} B -->|直连通畅| C[直接连接 Git 服务器] B -->|速度慢/超时| D[代理服务器] D --> E[Git 服务器github.comgitlab.com] C --> E 配置方式总览 Git 提供多种配置代理的途径,优先级从高到低: 配置方式 持久性 作用范围 -c 命令行参数 单次命令 单仓库 git config --local 写入 .git/config 单个仓库 git config --global 写入 ~/.gitconfig 当前用户 环境变量 Shell 会话级 当前终端 graph TD A[Git 发起网络请求] --> B{有 -c 参数?} B -->|是| F[使用 -c 指定的代理] B -->|否| C{有 local 配置?} C -->|是| G[使用 local 代理] C -->|否| D{有 global 配置?} D -->|是| H[使用 global 代理] D -->|否| E{有环境变量?} E -->|是| I[使用环境变量代理] E -->|否| J[直连] 一、HTTP/HTTPS 代理 这是最常用的方式,适用于 https:// 协议的仓库。 1.1 配置命令 # 全局设置 git config --global http.proxy http://127.0.0.1:7890 git config --global https.proxy http://127.0.0.1:7890 # 仅对当前仓库设置 git config --local http.proxy http://127.0.0.1:7890 # 查看当前配置 git config --global --get http.proxy # 取消代理 git config --global --unset http.proxy git config --global --unset https.proxy 1.2 带认证的代理 # 用户名密码认证 git config --global http.proxy http://user:pass@127.0.0.1:7890 # 特殊字符需要 URL 编码,@ → %40,: → %3A git config --global http.proxy http://user:p%40ss@127.0.0.1:7890 1.3 针对特定域名 只想给 GitHub 走代理,公司内网 GitLab 直连?按域名精准控制: ...

2026年8月11日 · 4 分钟 · 715 字