该问题对应的网上解决方式

问题描述

换电脑使用hexo发布博客,hexo源码是备份到github上的,clone到本地 hexo g 生成博客没有报错,hexo d发布博客出问题了,报出如下错误。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
> sudo hexo d
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
FATAL Something's wrong. Maybe you can find the solution here: http://hexo.io/docs/troubleshooting.html
Error: Permission denied (publickey).
fatal: Could not read from remote repository.

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

at ChildProcess.<anonymous> (/Users/youcanping/Desktop/MyBlog/node_modules/hexo-util/lib/spawn.js:37:17)
at emitTwo (events.js:125:13)
at ChildProcess.emit (events.js:213:7)
at maybeClose (internal/child_process.js:927:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5)

解决方式

  1. 先看本地是否有ssh文件

    1
    2
    3
    > cd ~/.ssh
    > ls
    id_rsa id_rsa.pub known_hosts
  2. 有则把公钥加到github

    1
    2
    >cat id_rsa.pub
    ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDVRgHi3gPdBcQ... xxxx@163.com
  3. github 添加公钥示意图

  4. 如果以上操作问题还不能解决,并且执行 ssh -T git@github.com 出现如下提示,说明本地公钥没有问题,则看第5步

    1
    2
    > ssh -T git@github.com
    Hi youcanping! You've successfully authenticated, but GitHub does not provide shell access.
  5. 看本地的.git/config设置的仓库url地址和github使用的链接地址是否一致如下图,如use https,则url需要用https的仓库地址,我的就是这个问题。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    > cat .git/config
    [core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = true
    [remote "origin"]
    url = https://github.com/youcanping/MyBlog.git
    fetch = +refs/heads/*:refs/remotes/origin/*
    [branch "master"]
    remote = origin
    merge = refs/heads/master
  6. 把公钥放到系统里[最佳答案]

    1
    2
    > ssh-add ~/.ssh/id_rsa
    > Identity added: /Users/youcanping/.ssh/id_rsa (/Users/youcanping/.ssh/id_rsa)