Fix – git@github.com : permission denied (publickey). fatal could not read from remote repository

2022.11.15

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Issue

While trying to clone to a repository, due to the lack of access to your computer's public key GitHub does not trust your computer and rejects your connection. When you attempt to clone the repository, you receive the following error message:

Command:

npx degit -m=git path/path my-app

Error:

! Command failed: git clone git@github.com:path/path my-app
Cloning into my-app...
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.

Solution

Step 1: Generate the SSH key

In the current directory's terminal:

ssh-keygen

Result A:

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/path/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

In the above result A:

  • For Enter file in which to save the key we can press enter and continue to save as the default name which is "id_rsa".
  • For Enter passphrase also we can press enter and continue or we can also provide a passphrase which is like a password.

Result B: On successful entry of result A, we are directed with the following output which shows the path of SSH keys.

Your identification has been saved in /Users/path/.ssh/id_rsa
Your public key has been saved in /Users/path/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:bWzCn***************************** user@*******.local
The key's randomart image is:
+---[RSA 3072]----+
|     ..+o   =*++o|
|   *****    *****|
|     . =.=+++... |
|   *****    *****|
|    . +   O.+o.  |
|   *****    *****|
|       .  .      |
+----[SHA256]-----+

Step 2: Copy the SSH key

cat /Users/path/.ssh/id_rsa.pub

Copy the SSH Key(contents) from the id_rsa.pub file.

Step 3: Add the SSH key to GitHub

  • Click on settings in the GitHub Account.
  • Click on `SSH and GPG keys.
  • Click on the NEW SSH key button.
  • Enter the title, paste the key value, and then click on add SSH key.

Conclusion

Now on running your clone or degit command, you should be able to clone to the repository without any issues. With this approach, you need not supply your Username and password to GitHub which is of course a more secure way.

Thank you.