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
    

非常的有品

以上

相关推荐

  1. Git | SSH 连接 GitHub

    2024-06-07 02:00:01       33 阅读
  2. 将SSH添加GitHub账户

    2024-06-07 02:00:01       24 阅读
  3. git ssh配置 & 本地项目推送github

    2024-06-07 02:00:01       47 阅读
  4. Github Gitlab SSH 配置

    2024-06-07 02:00:01       43 阅读
  5. github / gitlab s申城 配置 ssh key

    2024-06-07 02:00:01       52 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-07 02:00:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-07 02:00:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-07 02:00:01       82 阅读
  4. Python语言-面向对象

    2024-06-07 02:00:01       91 阅读

热门阅读

  1. lsof 命令

    2024-06-07 02:00:01       31 阅读
  2. Nacos控制台服务安装

    2024-06-07 02:00:01       26 阅读
  3. Meta Llama 3 大型语言模型的超参数

    2024-06-07 02:00:01       28 阅读
  4. 源代码先转字节码,再转机器码的过程

    2024-06-07 02:00:01       32 阅读
  5. 【redis】set和zset常用命令

    2024-06-07 02:00:01       27 阅读
  6. Go 语言的控制结构:条件与循环

    2024-06-07 02:00:01       31 阅读