本文最后更新于:2026年7月17日 下午

在国内服务器上开发时,git push 突然超时,SSH/HTTPS 全部连不上 GitHub。本文记录从诊断到修复的完整过程,核心原因是本地有 Clash 代理但 Git 和 SSH 没有配置使用它。

问题现象

git push 卡住直到超时:

1
2
3
$ git push origin main
ssh: connect to host github.com port 22: Connection timed out
fatal: Could not read from remote repository.

逐步排查

1. 确认 Remote 和分支状态

1
2
3
4
5
6
$ git remote -v
origin git@github.com:xxx/xxx.git (fetch)
origin git@github.com:xxx/xxx.git (push)

$ git branch -vv
* main fac17aa [origin/main: ahead 50] feat: ...

远程地址是 SSH 协议,本地 ahead 50 个 commit,需要 push。

2. 测试 SSH 连接

1
2
$ ssh -T git@github.com -o ConnectTimeout=10
ssh: connect to host github.com port 22: Connection timed out

SSH 端口 22 超时。尝试 443 端口(参考前文):

1
2
$ ssh -T -p 443 git@ssh.github.com -o ConnectTimeout=10
ssh: connect to host ssh.github.com port 443: Connection timed out

443 也超时,说明不是简单的端口封禁问题。

3. 测试 HTTPS 和 Ping

1
2
3
4
5
$ curl -s --connect-timeout 10 -o /dev/null -w "HTTP %{http_code} in %{time_total}s\n" https://github.com
HTTP 000 in 10.002881s

$ ping -c 2 -W 3 github.com
2 packets transmitted, 0 received, 100% packet loss

GitHub 完全不可达。国内网络验证正常:

1
2
$ curl -s --connect-timeout 10 -o /dev/null -w "HTTP %{http_code} in %{time_total}s\n" https://www.baidu.com
HTTP 200 in 0.234748s

结论:GitHub 被网络环境完全阻断。

4. 寻找本地代理

1
2
$ ss -tlnp | grep -E ':(1080|7890|7891|10808|10809)'
LISTEN 0 4096 127.0.0.1:7890 0.0.0.0:* users:(("clash-linux",pid=7459,fd=10))

Clash 在 127.0.0.1:7890 运行。验证代理能否访问 GitHub:

1
2
$ curl -s --connect-timeout 5 -x http://127.0.0.1:7890 -o /dev/null -w "HTTP %{http_code} in %{time_total}s\n" https://github.com
HTTP 200 in 3.519280s

HTTP 代理可用。

修复方案

需要配置两层代理:Git HTTPS 代理 + SSH over 代理。

1. 配置 Git HTTP/HTTPS 代理

适用于 HTTPS 协议的 remote(https://github.com/...):

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

2. 配置 SSH 走代理

当前 remote 是 SSH 协议(git@github.com:...),SSH 不走 http.proxy,需要额外配置。

编辑 ~/.ssh/config,在文件顶部添加:

1
2
3
4
Host github.com
HostName github.com
User git
ProxyCommand socat - PROXY:127.0.0.1:%h:%p,proxyport=7890

注意:需要系统安装 socat。如果没有可以用 nc(netcat)替代:

1
ProxyCommand nc -X connect -x 127.0.0.1:7890 %h %p

如果都没有,Ubuntu 上安装:sudo apt install socat

3. 验证与推送

1
2
3
4
5
6
$ ssh -T git@github.com -o ConnectTimeout=15
Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.

$ git push origin main
To github.com:xxx/xxx.git
7687894..fac17aa main -> main

推送成功。

补充说明

  • 取消代理:如果之后网络环境变化不需要代理了,清除配置:
1
2
git config --global --unset http.proxy
git config --global --unset https.proxy

然后删除 ~/.ssh/config 中的 Host github.com 块即可。

  • 仅对 GitHub 生效:上述 SSH 配置仅对 github.com 域名生效,不影响其他 SSH 连接。

  • SOCKS5 vs HTTP:经测试当前 Clash 的 SOCKS5 端口不可用(socks5://127.0.0.1:7890 超时),只有 HTTP 代理端口可用,实际使用时需确认自己代理工具的有效端口和协议。

续:配好代理后 SSH 改走 443(2026-07)

上一篇配的是让 SSH 经 Clash 代理走 github.com:22。三个月后 hexo d 又报 Spawn failed,这次不同:代理正常,22 端口的 SSH 连不上,443 可以。

现象

hexo d 本地生成正常,push 时报错:

1
2
3
4
5
6
7
8
9
10
11
kex_exchange_identification: Connection closed by remote host
Connection closed by UNKNOWN port 65535
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
FATAL Something's wrong. Maybe you can find the solution here: https://hexo.io/docs/troubleshooting.html
Error: Spawn failed
at ChildProcess.<anonymous> (/home/vvd/VVD/VVD_Hexo/node_modules/hexo-deployer-git/node_modules/hexo-util/lib/spawn.js:51:21)
at ChildProcess.emit (node:events:517:28)
at ChildProcess._handle.onexit (node:internal/child_process:292:12)

kex_exchange_identification 出现在 SSH 握手阶段。和上一篇的 Connection timed out 不同,这次是握手过程中连接被关闭。先确认是不是代理的问题。

逐步排查

1. 代理和 socat 是否正常

1
2
3
4
5
$ which socat
/usr/bin/socat

$ ss -tlnp | grep 7890
LISTEN 0 4096 *:7890 *:* users:(("clash-linux",pid=150594,fd=10))

socat 和 Clash(7890)都在。

2. 走代理访问 GitHub(HTTPS)

1
2
$ curl -s -o /dev/null -w "HTTP %{http_code}  time %{time_total}s\n" -x http://127.0.0.1:7890 https://github.com
HTTP 200 time 1.247209s

代理可用,问题不在代理本身。

3. 走代理做 SSH

1
2
3
$ ssh -T -o ConnectTimeout=8 github.com
Connection timed out during banner exchange
Connection to UNKNOWN port 65535 timed out

同一条代理,HTTPS 正常,SSH 在 banner 交换阶段超时。换成 443 再试(参考更早那篇):

1
2
$ ssh -T -o HostName=ssh.github.com -o Port=443 -o ProxyCommand="socat - PROXY:127.0.0.1:%h:%p,proxyport=7890" git@github.com
Hi zywvvd! You've successfully authenticated, but GitHub does not provide shell access.

443 可以。

结论

代理正常,22 端口的 SSH 经代理握手失败,443 可以。让 SSH 也走 443。

修复:把代理的 Host 块改走 443

之前 ~/.ssh/config 里是经代理连 github.com:22

1
2
3
4
Host github.com
HostName github.com
User git
ProxyCommand socat - PROXY:127.0.0.1:%h:%p,proxyport=7890

把目标改成 ssh.github.com:443

1
2
3
4
5
Host github.com
HostName ssh.github.com
User git
Port 443
ProxyCommand socat - PROXY:127.0.0.1:%h:%p,proxyport=7890

ProxyCommand 里的 %h/%p 取上面的 HostNamePort,所以 socat 实际连的是 ssh.github.com:443,ProxyCommand 不用改。

验证:

1
2
$ ssh -T github.com
Hi zywvvd! You've successfully authenticated, but GitHub does not provide shell access.

SSH 通了,hexo g -d 重新部署即可。

三次记录对比

时间 现象 原因 修复
2024-01 直连 SSH 22 超时 22 端口连不上 ssh.github.com:443
2026-04 直连 22、443 都超时 GitHub 不可达 挂 Clash + socat 让 SSH 走代理
2026-07 走代理后 SSH 22 握手失败 22 握手失败,443 可用 代理 + 443

参考资料



文章链接:
https://www.zywvvd.com/notes/tools/git/git-proxy-clash/git-proxy-clash/


“觉得不错的话,给点打赏吧 ୧(๑•̀⌄•́๑)૭”

微信二维码

微信支付

支付宝二维码

支付宝支付

Git 在国内网络环境下无法 Push 的排查与修复 —— 配置 Clash 代理
https://www.zywvvd.com/notes/tools/git/git-proxy-clash/git-proxy-clash/
作者
Yiwei Zhang
发布于
2026年4月17日
许可协议