Git Push&Pull 使用 SSH(含 443 端口)配置

Git Push&Pull 使用 SSH(含 443 端口)配置
xiaoyunhai适用系统:Windows 10/11(内置 OpenSSH)。
目标:完全摆脱代理,让 Git 走 SSH;若 22 端口被封,改走 ssh.github.com:443。
一、HTTPS vs SSH:为什么要改用 SSH?
- HTTPS:简单,但在受限网络下常被拦(443 直连也可能被深度检查),且需要凭证管理器或 PAT。
- SSH:更稳,可用
ssh.github.com:443绕过 22 封锁;配好一次,所有仓库免输密码。
二、清理任何“代理残留”(极重要)
很多人 Git 连不通,其实是还在走 127.0.0.1:7890/57890 的HTTP/SOCKS 代理。先清空:
1 | git config --global --unset http.proxy & rem 取消全局 HTTP 代理 |
3.2 如无则新建(推荐 Ed25519)
1 | ssh-keygen -t ed25519 -C "你的邮箱" & rem 按回车使用默认路径 ~/.ssh/id_ed25519 |
四、启动 ssh-agent 并加载私钥(Windows)
1 | sc config ssh-agent start=auto & rem 将 OpenSSH 代理设为开机自启(只需一次) |
五、把公钥添加到 GitHub
- 复制公钥内容:
type %USERPROFILE%\.ssh\id_rsa.pub(或id_ed25519.pub)。 - GitHub → Settings → SSH and GPG keys → New SSH key → 粘贴保存。
六、端口 22 被封怎么办?——走 ssh.github.com:443
6.1 创建/编辑 ~/.ssh/config(无扩展名的 config)
1 | notepad %USERPROFILE%\.ssh\config & rem 打开或新建配置文件(确保不是 config.txt) |
写入最小可用配置(完整粘贴,保存):
1 | Host github.com |
6.2 验证配置是否生效
1 | ssh -G github.com | findstr /I "hostname port identityfile" |
6.3 连通性测试
1 | ssh -T -p 443 git@ssh.github.com & rem 直测 443:首次会提示 yes/no,输入 yes |
出现:
1 | Hi <你的 GitHub 用户名>! You've successfully authenticated, but GitHub does not provide shell access. |
即成功。
若
ssh -T git@github.com仍去连 22,多半是 config 被保存成了 config.txt,务必改回无扩展名的config并重测。
七、把仓库远程从 HTTPS 改为 SSH(每个仓库都要改)
7.1 单个仓库修改
进入仓库根目录(例如 E:\Blog\hexo-source):
1 | git remote -v |
7.2 批量修改(可选)
PowerShell 脚本(保存为 rewrite-https-to-ssh.ps1):
1 | # 遍历这些本地仓库目录,自动把 https://github.com/xxx 改为 git@github.com:xxx |
八、Hexo 部署改用 SSH(避免 HTTPS/PAT)
站点根目录的 _config.yml(注意是站点级,非主题):
1 | deploy: |
发布流程:
1 | hexo clean & rem 清理 |
九、常见报错与“对症一条命令”
1) Failed to connect to 127.0.0.1:7890 / Proxy CONNECT aborted
- 原因:Git 仍在走本机代理(端口不存在或协议不匹配)。
- 解决:清理代理(见第二章),或改用 SSH。
2) ssh: connect to host github.com port 22: Connection timed out
- 原因:22 端口被封,或 config 未生效。
- 解决:按第六章使用
ssh.github.com:443;用ssh -G github.com核对端口是否为 443;检查~/.ssh/config不是config.txt。
3) Host key verification failed
- 原因:首次连接缺少 host key 或
known_hosts里记录不匹配。 - 解决:重新连接,看到提示时输入
yes;若冲突,编辑%USERPROFILE%\.ssh\known_hosts删除对应行再连。
4) Permission denied (publickey)
- 原因:GitHub 未添加你的公钥,或
ssh-agent未加载私钥。 - 解决:把
id_rsa.pub/id_ed25519.pub添加到 GitHub;执行ssh-add <私钥路径>;用ssh-add -l确认已加载。
5) VS Code 仍报 https://github.com/...
原因:仓库远程还是 HTTPS,或 VS Code 注入了代理。
解决:在仓库里执行
1
git remote set-url origin git@github.com:<你>/<仓库>.git
然后在 VS Code:Settings → Http: Proxy 置空,命令面板 “Git: Show Git Output” 确认日志使用
git@github.com:形式。
十、最终检查清单(一次通过)
1 | ssh -G github.com | findstr /I "hostname port identityfile" & rem 应为 ssh.github.com / 443 / 指向你的私钥 |
附:遇到“必须用 HTTPS”的环境怎么办?
极个别企业网络强制 HTTPS:
- 临时切回 HTTPS:
git remote set-url origin https://github.com/<你>/<仓库>.git,并使用 Git Credential Manager(或个人访问令牌 PAT)。 - 或在
~/.ssh/config里通过 HTTP CONNECT 代理转发 SSH(需要额外工具,如 netcat/proxytunnel;一般不推荐,复杂度高)。
SSH 一次配置,处处生效。关键点只有三个——
~/.ssh/config走ssh.github.com:443;ssh-agent中有你的私钥;- 每个仓库的
origin用git@github.com:...。
做到这三步,Git push/pull 将长期稳定。






