文章内容
2025/9/23 0:39:40,作 者: 黄兵
[email protected]: Permission denied (publickey)
在推送代码的时候,出现了错误:
[email protected]: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
1. 确认你是否配置过 SSH Key
在终端执行:
ls ~/.ssh
看看有没有 id_rsa、id_rsa.pub 或者 id_ed25519、id_ed25519.pub 这样的文件。
如果没有,需要先生成一个新的 SSH key。
2. 生成新的 SSH Key(如果没有的话)
执行以下命令:
ssh-keygen -t ed25519 -C "你的GitHub邮箱"
如果你的系统不支持 ed25519,可以用 rsa:
ssh-keygen -t rsa -b 4096 -C "你的GitHub邮箱"
一路回车就行(除非你想修改路径/设置密码)。
3. 添加 SSH Key 到 ssh-agent
启动 ssh-agent 并添加密钥:
eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_ed25519 # 如果是 RSA 就改成 ~/.ssh/id_rsa
4. 把公钥添加到 GitHub
执行:
cat ~/.ssh/id_ed25519.pub
复制输出的整行内容。
然后去 GitHub → 右上角头像 → Settings → SSH and GPG keys → New SSH key,粘贴进去并保存,具体可以参考这篇文章:github 本地服务器初始化的设置步骤。
5. 测试连接
执行:
ssh -T [email protected]
如果成功,你会看到类似:
Hi your-username! You've successfully authenticated, but GitHub does not provide shell access.
6. 再次推送
配置好之后重新推送:
git push origin main # 或 master,看你分支名
评论列表