Skip to content

Git Bash 使用 Clash 代理

🔹 设置代理

想自动生成不同 Shell 的代理命令,可以使用:代理配置生成器

bash
export HTTP_PROXY="http://127.0.0.1:7890"
export HTTPS_PROXY="http://127.0.0.1:7890"

建议

默认情况下,Clash 的本地代理端口为 7890。如果你修改了 Clash 的端口设置,请相应调整上述命令中的端口号。

建议优先使用 HTTP 协议。个人经验是命令行工具里 HTTP 代理通常比 SOCKS5 更稳定,兼容性也更好。

当然你也可以使用其他 IP 的代理,比如局域网内另一台机器的代理:http://192.168.31.233:7890

bash
export HTTP_PROXY="http://192.168.31.233:7890"
export HTTPS_PROXY="http://192.168.31.233:7890"

提示

Git Bash 使用的是 Linux/Unix 风格的环境变量设置方式,与 Windows CMD 不同。Git Bash 使用的是 Linux/Unix 风格的环境变量设置方式,与 Windows CMD 不同。

🔹 验证代理是否生效

bash
curl -I https://www.google.com
bash
git clone https://github.com/VincentZyu233/VincentZyu233

🔹 配置 Git 全局代理

bash
# HTTP 代理
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890

# SOCKS5 代理(推荐)# SOCKS5 代理(推荐)
git config --global http.proxy socks5://127.0.0.1:7891
git config --global https.proxy socks5://127.0.0.1:7891

## 🔹 取消 Git 代理## 取消 Git 代理
git config --global --unset http.proxygit config --global --unset http.proxy
git config --global --unset https.proxygit config --global --unset https.proxy

🔹 针对特定域名配置代理

如果只想为 GitHub 配置代理:

bash
# 只为 GitHub 配置代理
git config --global http.https://github.com.proxy socks5://127.0.0.1:7891

取消特定域名代理:

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

🔹 持久化配置

如果希望每次打开 Git Bash 都自动设置代理,可以将代理命令添加到 ~/.bashrc 文件中:

bash
echo 'export HTTP_PROXY="http://127.0.0.1:7890"' >> ~/.bashrc
echo 'export HTTPS_PROXY="http://127.0.0.1:7890"' >> ~/.bashrc
source ~/.bashrc

🔹 注意事项

  • ✅ 环境变量在当前终端会话中有效- ✅ 环境变量在当前终端会话中有效
  • ✅ 关闭终端后,临时设置会失效- ✅ 关闭终端后,临时设置会失效
  • ✅ Git 全局配置会持久保存- ✅ Git 全局配置会持久保存
  • ⚠️ 确保 Clash 正在运行且代理端口正确- ⚠️ 确保 Clash 正在运行且代理端口正确

🔹 相关链接