windows环境git(github) 使用代理ssh/http
公司网络或家里宽带直连 GitHub 经常超时、Connection reset,clone/push 卡死。本地开着 Clash、V2Ray 之类监听 127.0.0.1:10808 时,可以让 SSH 走代理,HTTPS 另配 git config(下文补充)。当时环境:Windows 10,Git for Windows,代理端口以你本机为准。
SSH:修改 ~/.ssh/config
路径一般是 C:\Users\你的用户名\.ssh\config,没有就新建。先装 connect 或 Git 自带的 connect.exe(部分发行版带),ProxyCommand 会用到。
全局走代理可以写:
ProxyCommand connect -S 127.0.0.1:10808 %h %p
其中 -S 代表使用 socks 代理。如果是 http 模式,则为 -H
后接代理地址,如 127.0.0.1:10808
%h、%p 是 SSH 传给代理的目标主机和端口,不要改成固定 github.com。
如果你只想修改特定的情况做配置,可以增加如下内容
Host github.com
User git
Port 22
Hostname github.com
ProxyCommand connect -S 127.0.0.1:10808 %h %p
只对 github.com 生效,Gitee、公司 GitLab 不受影响。测连通:
ssh -T [email protected]
出现 Hi xxx! You've successfully authenticated 说明 SSH 通了。
若仍失败,用 ssh -vT [email protected] 看卡在哪一步;常见是代理没开、端口写错、或公司防火墙拦 22 端口——后者可试 GitHub 的 443 端口 SSH(在 Host github.com 里加 Hostname ssh.github.com、Port 443,仍走同一条 ProxyCommand)。
踩坑
connect找不到:把connect.exe所在目录加入 PATH,或在ProxyCommand里写绝对路径。- SOCKS5 和 HTTP 代理别混用参数:SOCKS 用
-S,HTTP 用-H。 - 代理软件要开「允许来自局域网的连接」或本机监听,端口和 config 里一致。
- 多个
Host块时,更具体的规则放前面;Host *放全局代理时要小心误伤内网 Git。 - Git for Windows 升级后偶发找不到
connect,重装 Git 或单独下载 connect 即可。
HTTPS:git config 代理(补充)
远程地址是 https://github.com/... 时,SSH config 不管用,需要:
git config --global http.proxy http://127.0.0.1:10808
git config --global https.proxy http://127.0.0.1:10808
SOCKS 代理有的版本支持:
git config --global http.proxy socks5://127.0.0.1:10808
取消代理:
git config --global --unset http.proxy
git config --global --unset https.proxy
只对 GitHub 设代理可以用:
git config --global http.https://github.com.proxy http://127.0.0.1:10808
避免所有站点都走代理。git clone 很慢但 ping github.com 通,多半是 HTTPS 没配代理;ssh 能通而 https 不行,就按上面分开配。
大仓库 push 中途断线,除了代理稳定,还可调大缓冲:
git config --global http.postBuffer 524288000
这是缓解手段,根因还是网络。
参考链接
Getting git to work through a proxy server (in Windows)
版权声明: 本文首发于 指尖魔法屋-windows环境git(github) 使用代理ssh/http(https://blog.thinkmoon.cn/post/954-guide-environment-windows-git/) 转载或引用必须申明原指尖魔法屋来源及源地址!