• Git | SSH 密钥连接到 GitHub


    如是我闻: 在新电脑上想推送分支,结果遇到了这个错误

    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 在尝试使用 SSH 密钥连接到 GitHub 时遇到了问题。以下是解决此问题的一般步骤总结:

    1. 检查 SSH 密钥

    首先,打开终端,检查是否有一个 SSH 密钥对(公钥和私钥):

    ls ~/.ssh
    

    如果看到 id_rsaid_rsa.pub(或其他以 .pub 结尾的文件),说明已经有一个 SSH 密钥对。如果没有,需要生成一个新的 SSH 密钥对。

    2. 生成新的 SSH 密钥对(如果没有现有的)

    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    

    按提示操作,将密钥文件保存到默认位置(~/.ssh)。

    3. 添加 SSH 密钥到 SSH 代理

    确保 SSH 代理正在运行,然后将 SSH 私钥添加到 SSH 代理:

    eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/id_rsa
    

    4. 将 SSH 公钥添加到 GitHub

    需要将生成的公钥添加到我们的 GitHub 账户中:

    1. 打开公钥文件并复制内容:

      cat ~/.ssh/id_rsa.pub
      
    2. 登录到 GitHub。

    3. 转到 Settings -> SSH and GPG keys -> New SSH key

    4. 将复制的公钥内容粘贴到 “Key” 字段,并为密钥命名,然后点击 “Add SSH key”。

    5. 测试 SSH 连接

    测试是否能够成功连接到 GitHub:

    ssh -T git@github.com
    

    应该看到类似于以下的消息:

    Hi yourusername! You've successfully authenticated, but GitHub does not provide shell access.
    

    6. 尝试再次推送

    回到项目目录,再次尝试推送分支:

    git push origin your-branch-name
    

    其他注意事项

    • 确保在 GitHub 上添加的是正确的公钥文件内容。

    • 确保的 SSH 代理在运行并且已经添加了私钥。

    • 如果有多个 SSH 密钥,可能需要在 ~/.ssh/config 文件中指定特定的密钥用于 GitHub:

      Host github.com
          HostName github.com
          User git
          IdentityFile ~/.ssh/id_rsa
      

    非常的有品

    以上

  • 相关阅读:
    Python <算法思想集结>之抽丝剥茧聊动态规划
    认识进制
    艾美捷Immunochemistry FAM-FLICA Caspase-1检测方案
    【一起来学C++】————(11)STL之vector容器容器
    弹性数据库连接池探活策略调研(一)——HikariCP
    【Vue】插件的定义和使用
    ServletFileUpload获取上传的多个文件和数据
    WebGL 同时使用多幅纹理
    Swing基本组件的用法(一)
    通讯网关软件012——利用CommGate X2OPC实现MS SQL数据写入OPC Server
  • 原文地址:https://blog.csdn.net/weixin_50907960/article/details/139309621