MENU

Git Bash 始终连不上的解决办法

August 17, 2018 • Read: 301 • 技术

前言

无论公钥如何生成,如何添加,Git Bash 始终连不上。出现这个问题太频繁了,经常性的出问题,网上搜索的结果大都太分散且不全,而且有的完全是错误的方法,在此整理出最有效的解决办法。

现象

现象一: git pull出现 Permission denied (publickey).

$ git pull
git@gitee.com: Permission denied (publickey).
fatal: Could not read from remote repository.

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

现象二: git clone 竟然也是 Permission denied (publickey).

$ git clone git@github.com:LiuGuoGY/test.git testhub
Cloning into 'testhub'...
Warning: Permanently added the RSA host key for IP address '**.**.**.**' to the list of known hosts.
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

现象三: ssh -T git@gitee.comssh -T git@github.com 能连上,但是 git pushgit pull 却出现 Access denied: Unauthorized .

$ git push -u origin master
Acess denied: Unauthorized
fatal: Could not read from remote repository.

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

解决办法

第一步: 先查看添加的公钥,

$ ssh-add -l
  • 现象一:出现 Could not open a connection to your authentication agent. ,请移至第二步。
  • 现象二:出现 The agent has no identities. ,请移至第三步。

第二步: 打开身份验证代理,依次执行

$ ssh-agent
$ eval `ssh-agent -s`
  • 现象:最终会输出类似 Agent pid 14468 的结果,如果不是,请重试一次,此时重试第一步,应该出现第一步的现象二,说明身份验证代理已经打开,继续下一步。

第三步: 添加生成的公钥,

$ ssh-add ~/.ssh/id_rsa

注意正确填写公钥的路径,比如D盘则类似: /d/gittest/key/id_rsa

  • 现象:类似 Identity added: /d/gittest/key/id_rsa (/d/gittest/key/id_rsa) ,说明公钥成功添加到身份验证里了。

第四步: 大功告成了,此时就不应出现类似错误现象了,如果还出现 Access denied: Unauthorized ,只需要重新 clone 就行啦,终于可以愉快的 pullpush 了。

注意:如果只能 pull 却不能 push ,请确认是否有 权限,码云(gitee)需要在 账户 中添加公钥,而不是仓库中, GitHub 需要在添加公钥时勾选 Write 权限。

$ Hi Linger! You've successfully authenticated, but Gitee.com does not provide shell access.
$ git push
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 1.25 KiB | 1.25 MiB/s, done.
Total 6 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To github.com:LiuGuoGY/test.git
 * [new branch]      master -> master
Tags: Git