git - 来自 Visual Studio Code 和 Visual Studio(但不是 Git Bash)的“权限被拒绝”错误
问题描述
使用运行 Windows 10 的新工作笔记本电脑,我安装git
了Visual Studio Code
和Visual Studio 2019
. 在从我的个人 git 存储库(托管在github
)中对代码进行一些测试更改后,我正在尝试提交这些更改并将其推送到远程存储库。
我可以从执行所有Stage
和Commit
操作。但在 Git Gui、VS Code 和 VS 2019 中,两者都可以正常工作,但会失败并显示错误消息。Push
Git Bash
Stage
Commit
Push
对于 VS Code,错误是:
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
对于 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.
我该如何解决这个问题?
更新
@DaemonPainter 的评论有助于找到答案。我的私人 SSH 密钥文件被命名id_rsa_github
为id_rsa_github.pub
. Visual Studio 2019、Code 和 Git Gui 都希望这些被命名id_rsa
为id_rsa.pub
.
解决方案是添加一个config
文件,%HOMEPATH%\.ssh
其内容类似于:
host github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa_github
进行更改后,git push
可以在 Git Bash、Gut Gui、VS Code 和 Visual Studio 中使用。
解决方案
正如@DaemonPainter 指出的那样,一种可能性是缺少 SSH 身份和密钥。对于这个问题,这个描述如何添加 SSH 密钥的答案可能会很有趣。
我的问题原来是 SSH 密钥文件的命名。默认情况下,这些文件位于%HOMEPATH%\.ssh
文件名id_rsa
和id_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
这就解释了为什么它适用于Git Bash
.
要配置非标准命名 SSH 密钥,请执行以下步骤:
添加一个名为
%HOMEPATH%\.ssh\config
编辑
config
类似以下内容(用于#
添加评论):host github.com # My personal git repo for (something-cool) HostName github.com IdentityFile ~/.ssh/id_rsa_github
重启你的开发工具,比如
Visual Studio Code
推荐阅读
- flutter - 如何在使用路径提供程序时覆盖或更新 Flutter 中的缓存文件
- visual-studio-code - Visual Studio 代码:问题加载扩展
- c# - 在什么类上我可以找到类型 DbSet 的扩展名“Where”
EF Core 5.0? - c++ - 字符串 utf 16 到 utf 8
- c# - 有没有办法在 Visual Studio C# 中隐藏包的调试日志记录?
- rest - 使用微控制器/明文预编码的 POST 字符串制作 HTTP POST (REST API)
- r - 在 R 中使用 Keras 功能 API 将文本与分类特征相结合
- java - 尝试使用某些图像设置图像资源时应用程序崩溃
- python - 在数据框表中打印单列
- laravel - 如何从与模型相关的列中获取数据?