300 字
2 分钟
设置Git全局代理
2026-02-07 12:24:00

设置和取消 Git 全局代理的命令如下:

设置 Git 全局代理#

HTTP/HTTPS 代理#

Terminal window
# 设置 HTTP 代理
git config --global http.proxy http://127.0.0.1:7890
# 设置 HTTPS 代理
git config --global https.proxy http://127.0.0.1:7890
# 一次性设置(常用)
git config --global http.proxy socks5://127.0.0.1:7890
git config --global https.proxy socks5://127.0.0.1:7890

SOCKS5 代理(如 Clash)#

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

取消 Git 全局代理#

Terminal window
# 取消 HTTP 代理
git config --global --unset http.proxy
# 取消 HTTPS 代理
git config --global --unset https.proxy
# 一次性取消全部(推荐)
git config --global --unset http.proxy
git config --global --unset https.proxy

查看当前代理配置#

Terminal window
git config --global --get http.proxy
git config --global --get https.proxy
# 或查看所有配置
git config --global --list

注意事项#

  1. 端口号7890 是常用代理端口,实际使用时请替换为你的代理软件端口

  2. 仅针对特定网站:如果只需要为 GitHub 设置代理:

    Terminal window
    # 设置
    git config --global http.https://github.com.proxy socks5://127.0.0.1:7890
    # 取消
    git config --global --unset http.https://github.com.proxy
  3. 临时代理:也可以使用环境变量临时设置:

    Terminal window
    # 设置临时代理
    export http_proxy=http://127.0.0.1:7890
    export https_proxy=http://127.0.0.1:7890
    # 取消临时代理
    unset http_proxy
    unset https_proxy

验证代理是否生效#

Terminal window
# 测试连接
curl -I https://github.com
# 或
git clone https://github.com/username/repo.git

提示:代理设置只影响 Git 操作,不会影响其他网络连接。

设置Git全局代理
https://fuwari.wisansiiz.top/posts/设置git全局代理/
作者
Wisansiiz
发布于
2026-02-07
许可协议
CC BY-NC-SA 4.0