Windows 下的 Bash 环境设置代理

🌍 一、全局环境变量方式(推荐用于临时代理)

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:10808

2️⃣ 验证是否设置成功

echo $http_proxy
echo $https_proxy

3️⃣ 测试是否生效

curl https://ipinfo.io

如果返回的 IP 地址与你本地不同(或与代理服务器一致),说明代理生效。
若不生效,可以强制使用:

curl -x $http_proxy https://ipinfo.io

4️⃣ 临时取消代理

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:10808

2️⃣ 查看当前配置

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

3️⃣ 取消代理

git config --global --unset http.proxy
git config --global --unset https.proxy

💡 推荐组合使用场景

目的推荐做法
临时想让 curlnpmdotnet restore 等命令都走代理export http_proxyhttps_proxy
只想让 git clone/pull/push 走代理git config --global http.proxy
想永久生效export 命令加进 ~/.bashrc~/.bash_profile