linux服務器下搭建hexo個人博客04:GitHub創建hexo分支備份hexo源文件

hexo -d命令只會在GitHub上面部署生成的html,css,js等網頁相關文件,而hexo源文件則仍然保存在服務器/本機上,這不利於hexo博客的搬遷。實際上,我們可以在GitHub上的hexo博客倉庫裏創建一個hexo分支保存所有的hexo源文件,這樣以後在其它服務器上我們可以通過git clone實現hexo博客一鍵移植

在之前username.github.io倉庫裏新建hexo分支

點擊Branch輸入hexo,即可創建hexo分支
因爲我已經創好hexo分支了,所以這裏創建分支a示例:
在這裏插入圖片描述

將hexo設爲默認分支

爲了簡化後面的部署,建議hexo設爲默認分支
settings->Branches
在這裏插入圖片描述

服務器git clone該倉庫

在這裏插入圖片描述
指定目錄下clone該項目,這裏我選擇的是/opt/hexo_surce,hexo爲之前hexo源文件的目錄

[root@iZwz93tzhqv97y8089coonZ opt]# pwd
/opt
[root@iZwz93tzhqv97y8089coonZ opt]# git clone https://github.com/html1211/html1211.github.io.git hexo_source
[root@iZwz93tzhqv97y8089coonZ opt]# ls
hexo  hexo_source  nodejs
刪除hexo分支下原有內容

由於新建的hexo分支會與之前master分支內容一致(保存的是靜態網頁),hexo分支不需要存放這些文件

git  rm -r <file>

刪除所有倉庫文件(.git除外)

[root@iZwz93tzhqv97y8089coonZ hexo_source]# ls -a
.  ..  2020  about  archives  atom.xml  categories  css  .git  images  index.html  js  lib  live2dw  schedule  search.xml  tags
[root@iZwz93tzhqv97y8089coonZ hexo_source]# git rm -r 2020  about  archives  atom.xml  categories  css  images  index.html  js  lib  live2dw  schedule  search.xml  tags
設置git origin
git remote add origin https://github.com/username/username.github.io.git

在這裏插入圖片描述

複製hexo源文件到hexo_source目錄

檢查一下hexo目錄下是否有.git和,有的話刪除它,不然它會覆蓋hexo_source目錄下b的.git
本地hexo_source倉庫就會失效
同時,如果hexo某個子目錄存在.git文件,那麼這個子目錄將無法git add到緩存區提交到GitHub
一般來說
hexo/themes/next目錄會有.git文件(因爲之前是通過git clone下載的next主題)
hexo/source/lib/canvas-nest目錄會有.git(安裝了這個插件的話)
保險起見,查找所有.git文件

find / -name .git

如果是在hexo/目錄下的話,rm -rf刪除它們

爲了簡單,我們直接拷貝hexo目錄下所有文件至hexo_source

/bin/cp -rf hexo/. hexo_source
git提交hexo源文件

git add -f強制提交所有源文件(包括非版本控制文件)

[root@iZwz93tzhqv97y8089coonZ hexo_source]# ls
_config.yml  db.json  node_modules  package.json  package-lock.json  public  scaffolds  source  themes
[root@iZwz93tzhqv97y8089coonZ hexo_source]# git add -f _config.yml  db.json  node_modules  package.json  package-lock.json  public  scaffolds  source  themes
git commit -m "add hexo source files"

git提交文件至hexo分支

git push origin hexo

這樣,你的所有hexo源文件放在了參倉庫的hexo分支

移植新服務器

在新服務器下指定目錄git clone 倉庫hexo源文件

git clone https://github.com/html1211/html1211.github.io.git hexo

甚至可以刪除當前服務器下原來的hexo目錄,在當前服務器hexo_source目錄下
開發,hexo g生成靜態網頁
通過hexo d部署到倉庫的master分支
通過git add -ugit commit -m "" ,git push origin hexo部署到倉庫的hexo分支

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章