运行git push时出错,提示Permission denied (publickey)

错误:  在终端(terminal)下

                      执行git clone [email protected]:accountName/repository.git命令时不出错,

                      运行git push时出错,提示如下

                      Permission denied(publickey).

                      fatal: Could not read from remote repository.

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

 

    原因:  可能是没有与github上的账号成功建立**对。

 

    解决:  

                  【1】ssh-****** -t rsa -C "[email protected]"

                            注意,上述[email protected]是指github账户的注册邮箱

                  【2】ssh -v [email protected]

                            上述命令执行后,出现的提示最后两句是

                                  No more authentication methods to try.

                                  Permission denied (publickey).

                  【3】ssh-agent  -s

                            上述命令执行后,出现的提示最后两句是

                                  SSH_AUTH_SOCK=/tmp/ssh-GTpABX1a05qH/agent.404; export SSH_AUTH_SOCK;

                                  SSH_AGENT_PID=13144; export SSH_AGENT_PID;

                                  echo Agent pid 13144;

                  【4】ssh-add ~/.ssh/id_rsa   

                            上述命令执行后,出现提示 

                                  Identity added: . . . (这里是一些ssh key 文件路径)

                                  Could not open a connection to your authentication agent.

                  【5】若【4】中出现上述提示,则执行此步骤,否则执行【6】

                           eval `ssh-agent  -s`       回车

                           ssh-add ~/.ssh/id_rsa        回车

                  【6】cat  ~/.ssh/id_rsa.pub   (也可以用其他方式打开)   

                            上述命令执行后id_rsa.pub文件内容将输出到终端,复制里面的**(内容一般是以ssh-rsa 开头,以github账号的注册邮箱结尾的,全部复制下来)

                  【7】进入github账号,在settings下,选SSH and GPG keys, 点击new SSH key

运行git push时出错,提示Permission denied (publickey)

图1. 添加**

 

                           点击new SSH key后,在title栏里自定义名字,然后将上一步复制的**(以ssh-rsa 开头,以github账号的注册邮箱结尾的)粘贴到此处。

                           然后点击Add SSH Key

                  【8】ssh -T [email protected]      回车

                            提示: Hi ---! You've successfully authenticated, but GitHub does not provide shell access.