将hexo源文件备份到Git仓库新分支上

我在电脑上拉取hexo仓库时,发现拉取下来的是经过渲染的静态文件,而不是源文件。假设本地也没有源文件,那发布文章就得重新安装和配置hexo,为了以后省事,本文记录怎样使用Git创建一个新分支来备份源文件

版本

  • hexo:6.0.0
  • next:7.8.0

假设公网IP为 111.11.1.11 ,记得换成自己的

说明

如果在 master 分支上执行:

1
2
3
git add .
git commit -m 'llll'
git push

可能会遇到以下报错和提示:

1
2
3
4
5
6
7
8
To 111.11.1.11:/home/git/hexo.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to '111.11.1.11:/home/git/hexo.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

我是这样理解的,在 master 分支上,hexo d 推送的是静态文件,git push 推送的是源文件,远程和本地工作不一致才出现这种情况

如果我在新分支上 git push 源文件是不是就不冲突了

推送

进行以下操作前复制一份hexo项目作为练习

复制源文件

首先进入hexo目录,复制该目录下除 .deploy_git.git(如果有的话) 之外所有的 源文件

19

创建并切换到hexo分支

1
git checkout -b hexo

粘贴源文件

然后进入 .deploy_git 目录,删除 .git 之外的所有文件,然后粘贴 源文件

18

添加hexo分支到名为hexo.git的远程仓库

1
git remote add hexo 'git@111.11.1.11:/home/git/hexo.git'

查看本地和远程分支

1
git branch -a

20

将源文件推送到远程仓库hexo分支下

1
2
3
git add .
git commit -m 'hexo'
git push --set-upstream origin hexo

结果如下:

1
2
3
4
5
6
7
8
9
10
Enumerating objects: 417, done.
Counting objects: 100% (417/417), done.
Delta compression using up to 8 threads
Compressing objects: 100% (371/371), done.
Writing objects: 100% (417/417), 570.24 KiB | 2.02 MiB/s, done.
Total 417 (delta 37), reused 77 (delta 18), pack-reused 0
remote: Resolving deltas: 100% (37/37), done.
To 111.11.1.11:/home/git/hexo.git
* [new branch] hexo -> hexo
Branch 'hexo' set up to track remote branch 'hexo' from 'origin'.

拉取

拉取hexo分支下的源文件

1
git clone -b hexo git@111.11.1.11:/home/git/hexo.git

hexo-beifen

node_modules

拉取下来的源文件没有 node_modules 目录,但可以从其他地方复制过来,或者通过下面命令安装:

1
2
3
4
5
cd hexo
npm install -g hexo-cli
npm install hexo-deployer-git --save
npm install
hexo s

如果本地浏览器能访问 http://localhost:4000 就成功了

部署与推送

在hexo分支下:

  • hexo d 默认部署到 master 分支,因为站点文件 config.yml 最下面指定了 branch: master
  • git push 当然推送到 hexo 分支

因此hexo分支下可以执行以下命令而不需切换到master分支:

1
2
3
4
5
6
hexo clean
hexo g
hexo d
git add .
git commit -m 'hexo'
git push

执行上面命令后如果正常就可以删除原来的hexo练习项目,使用刚拉取的项目

问题故障

hexo

第一次拉取源文件后,执行 git push 也出现过:

1
2
3
4
5
6
7
8
To 111.11.1.11:/home/git/hexo.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to '111.11.1.11:/home/git/hexo.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

我的解决方法是删除本地所有的hexo项目(可能跟存在多个 .git 有关),然后重新拉取hexo分支,再执行就没出现了

next

遇到如下问题,如果next主题是clone下来的,删除复制过来的 themes/next/.git 目录即可
41