报错结果如下:
git@github.com: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
适用场景:在 Git Bash 中执行 git push / git pull / git clone 时提示权限被拒绝。
核心原理:配置 SSH 密钥,让 GitHub 识别你的电脑。
步骤 1:检查本地是否已有 SSH 密钥
在终端中执行以下命令,查看是否已经生成过密钥:
ls -al ~/.ssh
- 如果有文件:看到
id_rsa和id_rsa.pub(或id_ed25519等),说明密钥已存在,请跳过步骤 2,直接去步骤 3 复制公钥。 - 如果没有文件:提示 "No such file or directory",请继续步骤 2 生成新的密钥。

步骤 2:生成新的 SSH 密钥
如果检查后发现没有密钥,运行以下命令生成(请将邮箱替换为你自己的 GitHub 邮箱):
ssh-keygen -t ed25519 -C "[email protected]"
- 操作提示
- 出现
Enter file in which to save the key:直接按 回车 使用默认路径。 - 出现
Enter passphrase:建议 直接回车留空(为了方便免密操作);如果设置了密码,以后每次操作 Git 都会要求输入这个密码。
- 出现

步骤 3:将公钥复制到剪贴板
我们需要把公钥(.pub 文件)的内容发给 GitHub。
复制输出的全部内容(从 ssh-ed25519 开始,一直到你的邮箱地址结束)。
在 Git Bash 中查看公钥内容:
cat ~/.ssh/id_ed25519.pub





