git clone 支持在命令行临时设置代理,不需要改全局配置,主要有两种常用方式:


在这里插入图片描述

1. 在 git clone 命令里直接用 -c 指定代理

这样只对本次 clone 生效,不会影响其他 git 操作。

git -c http.proxy=http://127.0.0.1:7890 \
    -c https.proxy=http://127.0.0.1:7890 \
    clone https://github.com/xxx/yyy.git

如果是 SOCKS5 代理:

git -c http.proxy=socks5://127.0.0.1:7890 \
    -c https.proxy=socks5://127.0.0.1:7890 \
    clone https://github.com/xxx/yyy.git

2. 使用环境变量临时代理

只对当前终端会话有效,适合一次性操作:

# HTTP 代理
export http_proxy=http://127.0.0.1:7890
export https_proxy=http://127.0.0.1:7890

# SOCKS5 代理
export http_proxy=socks5://127.0.0.1:7890
export https_proxy=socks5://127.0.0.1:7890

git clone https://github.com/xxx/yyy.git

完成后取消代理:

unset http_proxy
unset https_proxy

建议

  • 如果你只是偶尔需要代理,用 方法 1git -c 临时)更安全,不会影响后续操作。
  • 如果频繁用代理,可以在 ~/.gitconfig 里配置 http.https://github.com.proxy

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐