git@github.com: 使用 yarn 安装 github js 包时权限被拒绝(公钥)

git@github.com: Permission denied (publickey) when using yarn to install github js package

今天在macOS(Intel Chip)中执行yarn命令时,报错如下:

$ yarn                                                                                                 ‹ruby-2.7.2›
yarn install v1.21.1
warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.
[1/4]   Resolving packages...
warning webpack > watchpack > watchpack-chokidar2 > chokidar > fsevents > node-pre-gyp@0.12.0: Please upgrade to @mapbox/node-pre-gyp: the non-scoped node-pre-gyp package is deprecated and only the @mapbox scoped package will recieve updates in the future
[2/4]   Fetching packages...
error Command failed.
Exit code: 128
Command: git
Arguments: ls-remote --tags --heads ssh://git@github.com/jiangxiaoqiang/js-wheel.git
Directory: /Users/dolphin/source/reddwarf/frontend/Cruise-Radar
Output:
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.
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
(base)

我在 package.json 中配置 js-wheel 如下:

"js-wheel": "jiangxiaoqiang/js-wheel",

为什么即使 repo 是 public 仍然告诉我权限被拒绝?我尝试使用此命令生成 rsa 密钥:

$ ssh-keygen -t rsa -b 4096 -C “balabala@gmail.com”                                              ‹ruby-2.7.2›
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/dolphin/.ssh/id_rsa):
/Users/dolphin/.ssh/id_rsa already exists.
Overwrite (y/n)?
(base)

我应该怎么做才能避免这个问题?

您需要首先检查 ssh -Tv git@github.com 是否对您进行身份验证,这意味着它的输出是否以带有您的 GitHub 用户名的问候消息结尾。

只要那行不通,git ls-remote ssh://git@github.com/... 也行不通。

替代方法是强制使用 HTTPS URL:

git config --global url.https://github.com/.insteadOf git@github.com:

这尤其适用于 public 存储库,因为不需要凭据。