解决git@github.com: Permission denied (publickey)

报错结果如下

bash 复制代码
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 密钥

在 PowerShell中执行以下命令,查看是否已经生成过密钥:

bash 复制代码
ls -al ~/.ssh
  • 如果有文件 :看到 id_rsaid_rsa.pub(或 id_ed25519 等),说明密钥已存在,请跳过步骤 2,直接去步骤 3 复制公钥。
  • 如果没有文件:提示"No such file or directory",请继续步骤 2 生成新的密钥。

步骤 2️⃣:生成新的 SSH 密钥

如果检查后发现没有密钥,运行以下命令生成(请将邮箱替换为你自己的 GitHub 邮箱):

bash 复制代码
ssh-keygen -t ed25519 -C "xxx@qq.com"
  • 操作提示
    • 出现 Enter file in which to save the key:直接按 回车 使用默认路径。
    • 出现 Enter passphrase:建议直接回车留空(为了方便免密操作);如果设置了密码,以后每次操作 Git 都会要求输入这个密码。

步骤 3️⃣:将公钥复制到剪贴板

我们需要把公钥(.pub 文件)的内容发给 GitHub。

  1. 在 Git Bash 中查看公钥内容:

    bash 复制代码
    cat ~/.ssh/id_ed25519.pub

    (如果是 RSA 密钥,请输入 cat ~/.ssh/id_rsa.pub)

  2. 复制 输出的全部内容(从 ssh-ed25519 开始,一直到你的邮箱地址结束)。


步骤 4️⃣:在 GitHub 上添加 SSH Key
  1. 登录 GitHub,点击右上角头像 -> Settings
  2. 点击左侧菜单的 SSH and GPG keys
  3. 点击 New SSH key 按钮。
  4. Title 填写一个名字(例如:My Windows PC)。
  5. Key 粘贴刚才复制的公钥内容。
  6. 点击 Add SSH key 保存。



步骤 5️⃣:启动 SSH Agent 并添加私钥

这一步是让 Git Bash 认识刚才生成的密钥。

  1. 启动 Agent

    (在 Git Bash 中执行):

    bash 复制代码
    eval "$(ssh-agent -s)"
  2. 添加私钥

    bash 复制代码
    ssh-add ~/.ssh/id_ed25519

    (如果是 RSA 密钥,请输入 ssh-add ~/.ssh/id_rsa)

    • 如果提示 Identity added,说明成功了。

✅ 最终验证

在 Git Bash 中输入以下命令测试连接:

bash 复制代码
ssh -T git@github.com
  • 如果看到 Hi username! You've successfully authenticated...,说明配置成功!现在你可以正常进行代码推送了。
相关推荐
笨鸟先飞,勤能补拙1 小时前
AI 赋能网络安全领域深度剖析
网络·人工智能·windows·安全·web安全·网络安全·github
gwf2164 小时前
SSD读写速度深度解析:顺序读写vs随机读写、IOPS、延迟,你的硬盘性能到底怎么看?
git·嵌入式硬件·缓存·github·智能硬件
笨鸟先飞,勤能补拙10 小时前
下一个十年:网络安全正在被重写
网络·人工智能·安全·web安全·网络安全·github
潘正翔11 小时前
k8s进阶_Harbor镜像仓库
git·云原生·容器·kubernetes·gitee·github
vibecoding7711 小时前
GitHub 2026 年 7 月热榜:累计 Star 总榜与月度飙星榜
人工智能·github·ai编程
Pniubi11 小时前
Gitee&GitHub同步仓库教程
gitee·github
TunerT_TQ12 小时前
GitHub深度工程评测:AI 系统提示词泄露知识库深度评测:6万星system_prompts_leaks情报资产背后,藏着什么?
人工智能·chatgpt·github·提示词工程·#valhalla静态工程审阅·systemprompt·大模型工程
Python私教12 小时前
GitHub Actions 实战:把测试、密钥扫描与 Docker 镜像检查接入 CI
ci/cd·docker·github·devsecops·github actions
茉莉玫瑰花茶13 小时前
TCP 全连接队列与 tcpdump 抓包
git·github·tcp·tcpdump
炸膛坦客14 小时前
Git 和 GitHub:(十四)rebase 到某个有新提交的远程仓库的分支
git·github