git@github.com:权限被拒绝(公钥)

Jes*_*sie 16 git github git-push

我创建了一个新的远程仓库,并试图使用git push -u origin master命令后,我对我的本地文件推到新的存储库中的第一次add它和commit它。然而,它弹出这个git@github.com: Permission denied (publickey). fatal: Could not read from remote repository.。我该如何修复这个致命错误?

我已经尝试过
使用 Git 时如何解决 Permission denied (publickey) 错误? 此链接中的问题似乎发生在第一次使用 git 时。我已经使用我的 git 一段时间了,我还需要遵循这个解决方案吗?谁能给我一个更具体的解决方案?

这是我得到的致命错误

C:\Users\ASUS\Desktop\React-Practice App\my-app>git status
On branch master
nothing to commit, working tree clean

C:\Users\ASUS\Desktop\React-Practice App\my-app>git push -u origin master
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.
Run Code Online (Sandbox Code Playgroud)

小智 52

我遇到这个问题是因为我像 @Mon 一样使用了非标准位置的密钥。就我而言,我没有使用默认文件名,因为我的系统上有多个密钥。所以单纯通过移动或者重命名是无法解决的。

解决方案是编辑或创建~/.ssh/config(OpenSSH 文档config配置身份密钥的位置)以包含以下内容:

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/github-key
Run Code Online (Sandbox Code Playgroud)
  • 不要编辑第 3 行来反映您自己的用户名。用户名应该是git(参见github 文档)。

  • 编辑第 4 行以反映密钥的位置。

如果您已经有一个config文件,那么在乱搞之前看看里面有什么是值得的。在编辑之前制作一份副本总不会有什么坏处。

上面假设你已经创建了密钥,把它放在github上,你实际上确实权限,其他一切都应该是这样,但仍然遇到错误。

  • 这就是我需要做的,因为我有一个非标准的名字 (2认同)

Eri*_*ric 24

即使在 github 上添加 ssh 密钥后也遇到同样的问题。

通过运行此命令修复它

ssh -vT git@github.com

  • 非常感谢!它显示了错误日志,并发现 git 在尝试进行身份验证时使用特定路径(~/.ssh)和文件名(id_<type>)。我不应该重命名它。 (5认同)

moa*_*zem 23

您正在通过 SSH 访问 GitHub。首先生成一个SSH密钥对;然后将公钥添加到 GitHub。

生成密钥对:

ssh-keygen -t rsa -b 4096 -C “youremail@example.com”
Run Code Online (Sandbox Code Playgroud)

https://help.github.com/en/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agenthttps://help.github.com查看更多/en/articles/adding-a-new-ssh-key-to-your-github-account

  • 我已经有一个 ssh 密钥,并且它是加密的。所以我需要首先通过添加它(在 Mac 上)来解锁它,如下所示:“ssh-add ~/.ssh/id_rsa”,其中“id_rsa”是 ssh 的密钥。然后输入我的加密密码。 (5认同)
  • 我使用这个命令 ```ls -al ~/.ssh``` 来检查我是否有现有的 ssh 密钥。结果我已经有一个了。因此,如果我已经有了一个,为什么还需要生成一个新的呢? (4认同)
  • 导航到 github 配置文件 > 设置 > SSH 和 GPG 密钥 > 新 SSH 密钥。在文本字段中粘贴“~/.ssh/id_rsa.pub”文件的内容。请参阅答案中的第二个链接了解更多信息。 (3认同)