[TOC] #### 1. 問題描述 --- 使用 git clone 拉取 github 倉庫時(shí)報(bào)錯(cuò) ``` $ git clone git@github.com:shinyboys/test.git Cloning into 'test'... 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. ``` ``` Warning: Permanently added the RSA host key for IP address '192.30.253.113' to the list of known hos ``` 當(dāng)使用 git push 將本地代碼推送到遠(yuǎn)程倉庫時(shí)也會(huì)報(bào)錯(cuò) ![](https://img.itqaq.com/art/content/df57553be856d0dab88c897d2ae6e21a.png) #### 2. 生成 SSH 公鑰 --- 這個(gè)應(yīng)該是很多 github 新手會(huì)遇到的問題,原因就是當(dāng)前電腦沒有權(quán)限操作 github 倉庫。解決辦法: 當(dāng)前電腦生成 ssh 密鑰文件,將 ssh 公鑰添加到 github 賬號(hào)上,然后就可以正常拉取、推送代碼了 執(zhí)行生成 ssh 密鑰的命令, 會(huì)有交互性操作,提示讓輸入密碼, 直接一路回車即可 ssh-keygen 用于生成、管理、轉(zhuǎn)換認(rèn)證秘鑰 參數(shù): -t,type 指定要?jiǎng)?chuàng)建的秘鑰類型,可取值: rsa1(SSH-1)、rsa(SSH-2)、dsa(SSH-2) ``` ssh-keygen -t rsa ``` 查看生成的 SSH 公鑰 ``` cat ~/.ssh/id_rsa.pub ``` ![](https://img.itqaq.com/art/content/bd724077a7b8197a6f7192127d7e4f39.jpg) mac 系統(tǒng)生成 ssh 公鑰 ![](https://img.itqaq.com/art/content/fa2f0ab0c2697ca2a96cc2c418b3ab34.png) #### 3. 在 github 上添加 SSH 公鑰 --- **前往個(gè)人中心設(shè)置 `Settings`** ![](https://img.itqaq.com/art/content/9e81e67acb8b0b3cb5b7830445dfd13c.jpg) **點(diǎn)擊左側(cè)欄目 `SSH and GPG keys`** ![](https://img.itqaq.com/art/content/9786b887c8dad15714858f750dc3786c.jpg) **將生成的 SSH 公鑰保存到 github 中, 建議標(biāo)題標(biāo)明 SSH 公鑰來源** ![](https://img.itqaq.com/art/content/65bcb53c98ca587e6cd42ef36df822dd.jpg) #### 4. 在 gitee 上添加 SSH 公鑰 --- ![](https://img.itqaq.com/art/content/3ac8567b9c0a331ed468e62245338b86.png) #### 5. 重新拉取項(xiàng)目 --- ``` git clone git@github.com:shinyboys/test.git ``` ![](https://img.itqaq.com/art/content/46e38aac59871ec2ca1c84dd45e6d520.jpg)