This is an explanation of the video content.
 用技术延续对ACG的热爱
97

 |   | 

如何解决 git@github.com permission denied (publickey). fatal could not read from remote repository
PS C:\Users\xxxx\Desktop\Git> git clone git@github.com:xxx/demo-repo.git
Cloning into 'demo-repo'...
The authenticity of host 'github.com (140.82.114.4)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'GitHub: Where the world builds software' (ED25519) to the list of known hosts.
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Github 拒绝连接,其原因有两个

  1. 这是你的私人仓库(repo)
  2. Github 不信任你的计算机,因为它没有你计算机的公钥(public key)

所以当你尝试克隆(clone)仓库的时候你就会得到以下错误信息

git@github.com: Permission denied (public key). 
fatal: Could not read from remote repository. 

Please make sure you have the correct access rights 
and the repository exists.

如何解决这个问题呢?

第一步:创建SSH Key pair

打开git bash,输入

提示输入秘钥名字和密码,为了方便起见,我这里选择不填

PS C:\Users\xxxx\Desktop\Git> ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (C:\Users\xxxx/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in C:\Users\xxxx/.ssh/id_rsa.
Your public key has been saved in C:\Users\xxxx/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:PQqtJLnXe3WyERS/faU9DQgN8NU6cfZJwHUROfzm5j0 xxxx@xxxxxxxx
The key's randomart image is:
+---[RSA 3072]----+
|        ..oo++++=|
|         . o+++=.|
Receiving objects: 100% (6/6), done.

可以看到秘钥生成在C:\Users\xxxx/.ssh/这个地方,文件名为id_rsa.pub

我们把文件地址C:\Users\xxxx/.ssh/id_rsa.pub复制一下,通过cat命令查看其内容

C:\Users\xxxx\Desktop\Git> cat "C:\Users\xxxx/.ssh/id_rsa.pub"
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCpaUcHFgHs+h+dKledqqbdYtbU8q43NNhiaOL1508XD1
+DmQDVpLV2bNI9ZZomqlh3nOkZ3IMbXzhrJNdK/nDH5RnKu0z/nob9+bgKJRyl0YMjNyDb1ICGt155Eix6
mRHMTYlcEVkULQFfoPdeb3IvH2ICHMgm/PpHImSRxbtLfcBp9pUtdJ8bW+dsxZ9Bd/jZSPwx4QmUfGPkVh
BUlcmE2tsrIicEa9yWLY7lGYdZeZN+KtVfNiYoRLOlKRAyhPP69TG9tU6XZ3GBJCVOysFK8vi9bAYOhMW6
SwyLsTV+yc2GA4/ZmZQXZDkHpQznZM0O5bERrKLTWEJplSrXUXznNx2EdhDn8JaqCGYzS/TZ3a1tp5F0G8
jM0BC9J5jj8c6iJh0NjJ1U3l2CgB6pB5wVh9L7toqNmybOzJ+nCTZ4Wq2NSSRq4xdxmEWFVKn9fp/YxXuJ
ibC4J9iY7AFPAgBnqm3t0QXZ4iQ1/m2MnCTd+2lsG/Sfrr2gsRvPUWEL4IM= xxxx xxxx@xxxxxxx

第二步:将SSH key添加到Github账户

先把上面的秘钥整段复制了

在github点击头像->setting

点击SSH and GPG keys

点击New SSH key

把刚刚复制的秘钥粘贴到Key里面

这时候可以先不点Add SSH key,我们测试一下

PS C:\Users\xxxx\Desktop\Git> git clone git@github.com:xxxx/demo-repo.git
Cloning into 'demo-repo'...
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.

依然提示Permission denied

回到Github,点击Add SSH key然后再试一遍上面的指令,现在克隆仓库就不再提示Permission denied了

PS C:\Users\xxxx\Desktop\Git> git clone git@github.com:xxxx/demo-repo.git
Cloning into 'demo-repo'...
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
Receiving objects:  66% (4/6)100% (2/2), done.
Receiving objects: 100% (6/6), done.
remote: Total 6 (delta 0), reused 0 (delta 0), pack-reused 0

97 开发工具 ↦ Git疑难杂症 __ 301 字
 Git疑难杂症 #1