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

适用系统: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/57890HTTP/SOCKS 代理。先清空:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
git config --global --unset http.proxy    & rem 取消全局 HTTP 代理
git config --global --unset https.proxy & rem 取消全局 HTTPS 代理
git config --unset http.proxy & rem 在仓库目录里,取消本仓库 HTTP 代理
git config --unset https.proxy & rem 在仓库目录里,取消本仓库 HTTPS 代理
````

> VS Code 里也要清空:**Settings → 搜索 proxy → Http: Proxy** 留空并重启 VS Code。

---

## 三、准备 SSH 密钥(有就复用)

### 3.1 检查是否已有密钥

```cmd
dir %USERPROFILE%\.ssh & rem 查看是否已有 id_rsa / id_ed25519
type %USERPROFILE%\.ssh\id_rsa.pub & rem 若存在,打印公钥内容以便复制

3.2 如无则新建(推荐 Ed25519)

1
ssh-keygen -t ed25519 -C "你的邮箱"    & rem 按回车使用默认路径 ~/.ssh/id_ed25519

四、启动 ssh-agent 并加载私钥(Windows)

1
2
3
4
sc config ssh-agent start=auto         & rem 将 OpenSSH 代理设为开机自启(只需一次)
net start ssh-agent & rem 启动代理服务(已启动会提示“已在运行”)
ssh-add %USERPROFILE%\.ssh\id_rsa & rem 加载私钥到 agent;若用 ed25519 改文件名
ssh-add -l & rem 列出已加载的密钥,确认成功

五、把公钥添加到 GitHub

  1. 复制公钥内容:type %USERPROFILE%\.ssh\id_rsa.pub(或 id_ed25519.pub)。
  2. 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
2
3
4
5
6
7
Host github.com
HostName ssh.github.com # 指向 GitHub 的 SSH-Over-HTTPS 域名
Port 443 # 走 443 端口,绕过 22 的封锁
User git
IdentityFile ~/.ssh/id_rsa # 或 ~/.ssh/id_ed25519 按你实际密钥修改
ServerAliveInterval 60
ServerAliveCountMax 120

6.2 验证配置是否生效

1
2
ssh -G github.com | findstr /I "hostname port identityfile"
rem 期望输出包含:hostname ssh.github.com / port 443 / identityfile 指向你的私钥

6.3 连通性测试

1
2
ssh -T -p 443 git@ssh.github.com      & rem 直测 443:首次会提示 yes/no,输入 yes
ssh -T git@github.com & rem 常规域名测试,此时会按 config 走 443

出现:

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
2
3
4
5
git remote -v
git remote set-url origin git@github.com:<你的用户名>/<你的仓库名>.git
git remote -v & rem 确认已变成 git@github.com:... 形式
git fetch origin & rem 拉取测试
git push -u origin main & rem 推送测试(分支名按实际替换)

7.2 批量修改(可选)

PowerShell 脚本(保存为 rewrite-https-to-ssh.ps1):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 遍历这些本地仓库目录,自动把 https://github.com/xxx 改为 git@github.com:xxx
# 使用方法:在 PowerShell 中 cd 到脚本所在目录,然后:.\rewrite-https-to-ssh.ps1

$repos = @(
"E:\Blog\hexo-source",
"E:\OtherProject"
)

foreach ($repo in $repos) {
Write-Host "==== Fixing $repo ===="
Set-Location $repo
$old = git remote get-url origin
$new = $old -replace '^https://github.com/', 'git@github.com:'
if ($old -ne $new) {
git remote set-url origin $new
git remote -v
} else {
Write-Host "No change needed."
}
}

八、Hexo 部署改用 SSH(避免 HTTPS/PAT)

站点根目录的 _config.yml(注意是站点级,非主题):

1
2
3
4
deploy:
type: git # hexo-deployer-git
repo: git@github.com:<你的用户名>/<你的 pages 仓库>.git
branch: main # 或 gh-pages,按你的 Pages 设置

发布流程:

1
2
3
hexo clean   & rem 清理
hexo g & rem 生成静态文件
hexo d & rem 通过 SSH 推送到 repo

九、常见报错与“对症一条命令”

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
2
3
4
5
ssh -G github.com | findstr /I "hostname port identityfile"  & rem 应为 ssh.github.com / 443 / 指向你的私钥
ssh -T git@github.com & rem 成功提示 You've successfully authenticated...
git remote -v & rem 每个仓库远程应为 git@github.com:...
git fetch origin & rem 拉取无误
git push -u origin <branch> & rem 推送无误

附:遇到“必须用 HTTPS”的环境怎么办?

极个别企业网络强制 HTTPS:

  • 临时切回 HTTPS:git remote set-url origin https://github.com/<你>/<仓库>.git,并使用 Git Credential Manager(或个人访问令牌 PAT)。
  • 或在 ~/.ssh/config 里通过 HTTP CONNECT 代理转发 SSH(需要额外工具,如 netcat/proxytunnel;一般不推荐,复杂度高)。

SSH 一次配置,处处生效。关键点只有三个——

  1. ~/.ssh/configssh.github.com:443
  2. ssh-agent 中有你的私钥;
  3. 每个仓库的 origingit@github.com:...
    做到这三步,Git push/pull 将长期稳定。