换了电脑怎么更新 hexo 博客?

前言

差不多有两个月没更新 blog 了,写博客就像买东西,东西可以什么都买,也可以什么不买,博客也一样,可以什么都写,也可以什么都不写,现在回首看看以前的博客,虽然写得一般,但是重在坚持,还是稍稍有点成就感的。所以还是继续多写写一般般的博客吧 : )。

原来的我的 Hexo 博客网站源文件一直存放在自己笔记本上,现在入职了新公司,公司配了新的笔记本,想在公司笔记本上也能发布博文,所以想找个好方法实现多终端更新博客。我在知乎上发现一个利用分支托管源文件的 好答案

出师不利

利用分支的思路,在 GitHub 上新建一个hexo 分支来存放 Hexo 网站源代码,master 分支还是存放静态网页。所以我们需要将 Hexo 网站源代码推送到 hexo 分支。

我将自己笔记上的 Hexo 网站源文件夹复制到新笔记本后,在一开始 git remote add origin 连接 GitHub 远程库的时候就出现以下错误:

ERROR: Permission to DeppWang/deppwang.github.io.git denied to amyyanjie.
fatal: Could not read from remote repository.

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

根据错误意思显示是拒绝 “amyyanjie” 连接远程库。可能是没有正确连接或仓库不存在。我直接就 Google,发现原因可能是没有添加 SSH Key。其实还是应该先尝试通过错误提示来解决问题,首先仓库是一定存在的,那一定是连接有问题,所以就应该想到 SSH Key 出问题。

“amyyanjie” 是我安装 git 的时候新建的用户名,我已经将电脑唯一的 SSH Key 用于另一个 GitHub 账号,所以当我想添加 SSH key 到我的 GitHub 上时,就会报 SSH Key 已使用的错误。解决方法有两种:

  1. 找到你添加 SSH Key 的另一个 GitHub 账号,将对应的 SSH Key 删除,如果你不记得那个账号了,可以采用第二种方法;
  2. 如果你的电脑没有跟很多托管网站连接,采用简单粗暴的方法,重新生成一个 key!生成命令跟第一次一样 ssh-keygen -t rsa -C "[email protected]"。如果这个 SSH Key 还有用,你可以使用 ssh-keygen -t rsa_2 -C "[email protected]" 生成第二个 SSH Key。这样你可以实现一个电脑连接多个 GitHub 账号,具体参考 Git 之同一台电脑连接多个远程仓库

重新设置仓库用户名的命令是:git config --global user.name "yourname"

clone 远程非 master 分支

分支 push 上去之后,我们需要在新电脑上将 hexo 分支的内容 clone 下来,有两种方法:

1. 直接 clone 仓库,再切换 hexo 分支

直接 clone 仓库,此时默认显示 master 分支,但其实我们在 clone 时,已经将仓库所有的分支都 clone 下来了,我们只需要切换分支就行。具体命令如下:

  1. cd path 切换需要 clone 的文件夹路径
  2. git clone [email protected]:yourname/yourname.github.io.git Blog Blog 为文件夹别名;github 不区分用户名大小写
  3. git checkout -b hexo origin/hexo 将远程分支 hexo 切换为本地分支 hexo
  4. 第三步也可以使用 git checkout origin/hexogit checkout -b hexo 来达到同样的目的

2.github 设置 hexo 分支为默认分支,直接 clone

打开项目,在 Settings 中找到 Branchs,将 hexo 设为默认分支,再 update。

发布更新博客

将分支内容 clone 下来后,注意此时分支是 hexo,我们就要安装 hexo 和 npm 来发布博文,具体命令如下:

  1. npm install hexo --save 安装 hexo
  2. npm install 安装 npm,安装当前目录 package.json 文件中配置的 dependencies 模块
  3. npm hexo-deploy-git 安装支持使用 git 发布博文的插件 (npm install –save hexo-deployer-git)
  4. 注意:不用 hexo init,因为此条命令是用于新建所需要的文件,这里不需要新建。
  5. npm ls --depth 0 查看 npm 安装各 hexo 插件的情况

发布博文出现以下错误提示

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: Warning: Permanently added the RSA host key for IP address '123.59.85.188' to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

根据描述可以看出是 IP 有问题,’123.59.85.188’ 没有永久添加在.ssh 文件夹下的 known_hosts 中。通过 ssh -T [email protected] 命令测试显示以下语句:

Hi DeppWang! You've successfully authenticated, but GitHub does not provide shell access.

说明已经成功连接,但是没有提供 shell 访问权。

解决

最后发现是我博客连了 CodingGithub 两个网站,而我的新电脑的 SSH Key 只在 Github 上添加了的。在 Coding 上添加 SSH Key 就好了

参考资料

deppwang wechat

评论默认使用 ,你也可以切换到 来留言。