Visual Studio Code 和 Visual Studio(但不是 Git Bash)出现“权限被拒绝”错误

Ala*_*inD 9 git visual-studio-code visual-studio-2019

使用运行 Windows 10 的新工作笔记本电脑,我安装了gitVisual Studio CodeVisual Studio 2019. 在对我的个人 git 存储库(托管在 )中的代码进行一些测试更改后github,我尝试提交这些更改并将其推送到远程存储库。

我可以执行 中的所有StageCommitPush操作Git Bash。但在 Git Gui、VS Code 和 VS 2019 中,Stage两者Commit都工作正常,但Push失败并显示错误消息。

对于 VS Code,错误是:

git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Run Code Online (Sandbox Code Playgroud)

对于 VS 2019,错误类似:

git@github.com: Permission denied (publickey).
Error encountered while pushing to the remote repository: Git failed with a fatal error.
Could not read from remote repository.
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

更新

@DaemonPainter 的评论帮助找到了答案。我的 SSH 私钥文件被命名为id_rsa_githubid_rsa_github.pub. Visual Studio 2019、Code 和 Git Gui 都希望它们被命名id_rsaid_rsa.pub.

解决方案是添加一个内容类似于以下内容的config文件:%HOMEPATH%\.ssh

host github.com
 HostName github.com
 IdentityFile ~/.ssh/id_rsa_github
Run Code Online (Sandbox Code Playgroud)

进行更改后,git push可在 Git Bash、Gut Gui、VS Code 和 Visual Studio 中使用。

Ala*_*inD 6

正如 @DaemonPainter 指出的,一种可能性是 SSH 身份和密钥丢失。对于这个问题,描述如何添加 SSH 密钥的答案可能会令人感兴趣。

我的问题是 SSH 密钥文件的命名。默认情况下,这些文件位于%HOMEPATH%\.ssh文件名id_rsaid_rsa.pub. 开发人员工具(例如Visual Studio 2019、 )并Git Gui期望采用这种命名约定。

由于我的文件用于个人 git 存储库Github,因此我将 SSH 密钥文件命名为id_rsa_github/.pub。在 中%HOMEPATH%\.bashrc,添加了以下几行(最初在另一台 PC 上配置并复制过来):

# Start ssh-agent to allow git to be used
eval `ssh-agent -s`
ssh-add ~/.ssh/id_rsa_github
Run Code Online (Sandbox Code Playgroud)

这解释了为什么它有效Git Bash

要配置非标准命名 SSH 密钥,请按照下列步骤操作:

  1. 添加一个名为%HOMEPATH%\.ssh\config

  2. 编辑config类似以下内容(用于#添加注释):

    host github.com
     # My personal git repo for (something-cool)
     HostName github.com
     IdentityFile ~/.ssh/id_rsa_github
    
    Run Code Online (Sandbox Code Playgroud)
  3. 重新启动您的开发工具,例如Visual Studio Code