1️⃣ 确保 SSH Key 生成成功
运行:
ls ~/.ssh/
看看是否有 id_rsa
和 id_rsa.pub
这两个文件。如果没有,说明 SSH Key 还没有生成,需要重新生成:
ssh-keygen -t rsa -b 4096 -C "你的GitHub邮箱"
然后一直按 回车 (默认存放在 ~/.ssh/id_rsa
)。
2️⃣ 确保 SSH Key 已添加到 GitHub
运行:
cat ~/.ssh/id_rsa.pub
复制 整个公钥内容 (从 ssh-rsa
开头,到结尾)。然后:
-
点击 New SSH Key
-
粘贴公钥
-
保存
3️⃣ 让 SSH 识别你的 Key
运行:
eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_rsa
然后重新测试 SSH 连接:
ssh -T [email protected]
如果成功,你会看到:
Hi your-username! You've successfully authenticated, but GitHub does not provide shell access.
4️⃣ 确保 GitHub 远程仓库 URL 使用 SSH
运行:
git remote -v
如果看到的是 HTTPS:
origin https地址
https地址
那就需要改成 SSH:
git remote set-url origin SSH地址
然后尝试:
git push origin main