Git & SSH 使用代理

Git & SSH 使用代理

写在前面

Git 是每天都要使用的工具,但是直连 Github 一般都非常不稳定且速度慢。如果有条件,务必为自己的 Git 配置好代理。

提示

值得注意的是,如果你的 Git 使用 HTTPS 连接,那么需要设置 Git 代理。

如果使用 SSH 连接,那么需要设置 SSH 代理。

Git 全局代理

我直接设置全局,因为代理软件中设置了相关规则。也可以单独为某些域名设置代理,需要的自行查阅资料。

1
2
Git config --global http.proxy http://127.0.0.1:7890
Git config --global https.proxy https://127.0.0.1:7890

http://127.0.0.1:7890 可以改为 socks5://127.0.0.1:7890,因为某 C 开头的软件默认提供混合端口。

SSH

Https 连接时,每次拉取私库或者推送代码时,都需要输入 Github 的账号和密码(只能用软件代替输入)。而使用 SSH 则只要上传公钥即可,方便许多。

SSH 的代理需要在 ~/.ssh/config 中配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Windows用户,注意替换你的端口号和connect.exe的路径(可以用everything搜)
ProxyCommand "C:\path\to\bin\connect" -S 127.0.0.1:7890 -a none %h %p

# MacOS用户用下方这条命令,注意替换你的端口号
# ProxyCommand nc -v -x 127.0.0.1:7890 %h %p

Host github.com
User git
Port 22
Hostname github.com
# 注意替换IdentityFile
IdentityFile "C:\Users\AkiChase\.ssh\github"
TCPKeepAlive yes

Host ssh.github.com
User git
Port 443
Hostname ssh.github.com
# 注意替换IdentityFile
IdentityFile "C:\Users\AkiChase\.ssh\github"
TCPKeepAlive yes

配置完可以测试一下 SSH 是否能访问 Github:

1
2
# 测试是否设置成功
ssh -T [email protected]

提示

如果使用 Github Desktop,可以在仓库中设置修改远程仓库地址,从而走 SSH 而不是 HTTPS:

Repository -> Repository settings,将 https://github.com/ 改为 [email protected]:

   Vector Landscape Vectors by Vecteezy
作者

AkiChase

发布于

2024-01-06

更新于

2024-01-09

许可协议