hexo 博客源文件管理,实现备份、远程修改发布

一、全自动部署

可以实现通过一个仓库管理整套hexo博客的备份、部署、发布
总体参考博客: https://blog.linioi.com/posts/9/

其中关键的workflows控制文件(我修改过后的【参考】)

更改行数:

1
2
3
4
26        uses: actions/setup-node@v4
28        node-version: "14.x"
32        git clone -b master https://github.com/fluid-dev/hexo-theme-fluid.git themes/fluid
56        git checkout main

Git 详解文章:

二、两个仓库分别管理

1、搭建部分

注意: 博客源文件拉取出来后不能直接使用 hexo 命令,因为环境和依赖没有安装

a. 建立仓库

建立两个仓库

b. Git 初始化

将你的hexo本地文件初始化为git版本管理库

1
2
cd /{你的hexo博客目录}/
git init

c. 添加origin仓库地址

将本地hexo文件版本库与远程仓库建立链接(刚才建立的第二个仓库)

1
2
git remote add origin [email protected]:pppfz/blog_source.git
#由于我是用的SSH方式与GitHub验证所以选择HTTPS旁边的SSH地址(图片有误)

GitHub仓库链接获取

git remote 命令相关语法 以下是 git remote 命令的常见用法:
  • git remote:列出当前仓库中已配置的远程仓库。
  • git remote -v:列出当前仓库中已配置的远程仓库,并显示它们的 URL。
  • git remote add <remote_name> <remote_url>:添加一个新的远程仓库。指定一个远程仓库的名称和 URL,将其添加到当前仓库中。
  • git remote rename <old_name> <new_name>:将已配置的远程仓库重命名。
  • git remote remove <remote_name>:从当前仓库中删除指定的远程仓库。
  • git remote set-url <remote_name> <new_url>:修改指定远程仓库的 URL。
  • git remote show <remote_name>:显示指定远程仓库的详细信息,包括 URL 和跟踪分支。
    以下列出了远程仓库、添加远程仓库、重命名远程仓库、删除远程仓库、修改远程仓库 URL 和查看远程仓库信息的用法:

参考文章: https://www.runoob.com/git/git-remote.html

d. 将主题子模块化管理

将 hexo 主题添加为 git 子模块

1
2
3
4
5
6
#{}中间的链接改为你主题GitHub仓库的链接地址
git submodule add {https://github.com/fluid-dev/hexo-theme-fluid.git} ./themes/fluid

#最后两条命令二选一
git config push.recurseSubmodules check
git config push.recurseSubmodules on-demand #推荐这条

默认情况下,子模块会将子项目放到一个与仓库同名的目录中, 如果你想要放到其他地方,那么可以在命令结尾添加一个不同的路径(就像我这么用)。

git 发布子模块改动 参考说明 **发布子模块改动**

现在我们的子模块目录中有一些改动。 其中有一些是我们通过更新从上游引入的,而另一些是本地生成的,由于我们还没有推送它们,所以对任何其他人都不可用。

1
2
3
4
5
6
7
$ git diff
Submodule DbConnector c87d55d..82d2ad3:
> Merge from origin/stable
> updated setup script
> unicode support
> remove unnecessary method
> add new option for conn pooling

如果我们在主项目中提交并推送但并不推送子模块上的改动,其他尝试检出我们修改的人会遇到麻烦, 因为他们无法得到依赖的子模块改动。那些改动只存在于我们本地的拷贝中。

为了确保这不会发生,你可以让 Git 在推送到主项目前检查所有子模块是否已推送。 git push 命令接受可以设置为 “check” 或 “on-demand” 的 --recurse-submodules 参数。 如果任何提交的子模块改动没有推送那么 “check” 选项会直接使 push 操作失败。

1
2
3
4
5
6
7
8
9
$ git push --recurse-submodules=check
The following submodule paths contain changes that can
not be found on any remote:
DbConnector
Please try
git push --recurse-submodules=on-demand
or cd to the path and use
git push
to push them to a remote.

如你所见,它也给我们了一些有用的建议,指导接下来该如何做。 最简单的选项是进入每一个子模块中然后手动推送到远程仓库,确保它们能被外部访问到,之后再次尝试这次推送。 如果你想要对所有推送都执行检查,那么可以通过设置 git config push.recurseSubmodules check 让它成为默认行为。

另一个选项是使用 “on-demand” 值,它会尝试为你这样做。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ git push --recurse-submodules=on-demand
Pushing submodule 'DbConnector'
Counting objects: 9, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (9/9), 917 bytes | 0 bytes/s, done.
Total 9 (delta 3), reused 0 (delta 0)
To https://github.com/chaconinc/DbConnector
c75e92a..82d2ad3 stable -> stable
Counting objects: 2, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 266 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 0 (delta 0)
To https://github.com/chaconinc/MainProject
3d6d338..9a377d1 master -> master

如你所见,Git 进入到 DbConnector 模块中然后在推送主项目前推送了它。 如果那个子模块因为某些原因推送失败,主项目也会推送失败。 你也可以通过设置 git config push.recurseSubmodules on-demand 让它成为默认行为。

2、使用方法

默认使用情景:在一台新电脑中(GitHub连接成功、hexo安装完毕、git命令配置正确)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#第一步自检操作(目录要正确)
cd /../{hexo}/
git remote -v
git status

#克隆主项目和子项目
git clone --recurse-submodules [email protected]:pppfz/blog_source.git

......... 写完博客 .......... 正常发布 .............


#将博客源文件提交到GitHub仓库备份
git status
git add .
git commit -m "提交博客xxxx"
git push origin main:main
git push 命令用法

git push 命令用于从将本地的分支版本上传到远程并合并。

命令格式如下:

git push <远程主机名> <本地分支名>:<远程分支名>

如果本地分支名与远程分支名相同,则可以省略冒号:

git push <远程主机名> <本地分支名>

实例

以下命令将本地的 master 分支推送到 origin 主机的 master 分支。

1
git push origin master

相等于:

1
git push origin master:master

如果本地版本与远程版本有差异,但又要强制推送可以使用 –force 参数:

1
git push --force origin master

参考文章: https://www.runoob.com/git/git-push.html

参考文章:


hexo 博客源文件管理,实现备份、远程修改发布
https://blog.pppfz.com/posts/28.html
作者
PPPFZ
发布于
2024年6月4日
许可协议