🌍 一、全局环境变量方式(推荐用于临时代理)
Git Bash 是基于 MinGW + Bash 的,因此语法与 Linux 一致。
1️⃣ 设置 HTTP / HTTPS 代理
export http_proxy=http://127.0.0.1:10808
export https_proxy=http://127.0.0.1:10808如果你的代理需要用户名密码:
export http_proxy=http://user:password@127.0.0.1:108082️⃣ 验证是否设置成功
echo $http_proxy
echo $https_proxy3️⃣ 测试是否生效
curl https://ipinfo.io如果返回的 IP 地址与你本地不同(或与代理服务器一致),说明代理生效。
若不生效,可以强制使用:
curl -x $http_proxy https://ipinfo.io4️⃣ 临时取消代理
unset http_proxy
unset https_proxy🔧 二、Git 自身的代理设置(推荐用于 Git 命令)
Git 不一定会读取系统环境变量,因此可以单独设置:
1️⃣ 设置全局 Git 代理
git config --global http.proxy http://127.0.0.1:10808
git config --global https.proxy http://127.0.0.1:108082️⃣ 查看当前配置
git config --global --get http.proxy
git config --global --get https.proxy3️⃣ 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy💡 推荐组合使用场景
| 目的 | 推荐做法 |
|---|---|
临时想让 curl、npm、dotnet restore 等命令都走代理 | 用 export http_proxy 和 https_proxy |
只想让 git clone/pull/push 走代理 | 用 git config --global http.proxy |
| 想永久生效 | 把 export 命令加进 ~/.bashrc 或 ~/.bash_profile |