如何给 Git 设置代理

概述

如果你发现连接 GitHub 仓库越来越困难了,那你可能需要考虑挂代理访问了。

操作方法

HTTPS 协议

如果你使用 HTTPS 协议来访问 GitHub 仓库(remote 地址形如 https://github.com/your-name/your-repo.git),则可以给 Git 设置一个 HTTP 代理:

# 设置 https 代理
git config --global https.proxy http://127.0.0.1:1080

# 取消代理
git config --global --unset https.proxy

注:上述代码假设你架设的代理服务在 1080 端口。

SSH 协议

如果你使用 SSH 协议来访问 GitHub 仓库(remote 地址形如 git@github.com:your-name/your-repo.git),则需要给 SSH 指定 socks 代理。

找到 ~/.ssh/config 文件(如果没有就新建),添加如下内容:

Host github.com
ProxyCommand nc -X 5 -x 127.0.0.1:1080 %h %p

注:上述代码假设你架设的代理服务在 1080 端口。

注:Windows 系统需要自行安装 netcat 才有 nc 命令。

标签: Git
最后更新:2023-02-12 13:14:07