1. 挂载目录#

部署博客, 根目录下有两类文件:

  • 关于笔记, 主题相关的文件
  • public 文件夹( hugo 模板自动生成的博客静态文件)

我不想把这两类文件放一起, 分别建仓库又麻烦, 我想在一个仓库的两个分支分别存这两个内容:

  • hugo-blog 存放 markdown 和 主题配置相关文件
  • master 分支存储 public 文件夹下的文件, 用于 GitHub Pages

于是, 可以这么干

提交 笔记主题等文件到 hugo-blog 分支

git checkout -b hugo-blog
echo "public/" >> .gitignore  # 不追踪 public 文件夹
git add .
git commit -m "Add Hugo source files"

准备 master 分支,用来部署 public 目录里的内容

# 初始化主分支
git checkout --orphan master
# 清空当前目录所有文件(只对 git 管理的文件有效)
git rm -rf .

# 将 public 目录复制到当前目录(临时做法)
cp -r public/* .

# 添加并提交
git add .
git commit -m "Deploy Hugo site"

# 可选:推送到远程仓库
git push origin master --force

git checkout --orphan master

如果一个项目的 Git 历史非常混乱(比如有很多无用提交、错误的 merge、测试文件没删干净等)我们就可以通过这个指令创建一个干净的新开始,等于是

  • 创建一个新的“孤立分支” orphan branch, 这个分支 没有历史记录,就像是一个全新的仓库起点
  • 不带历史;重新选择想保留的文件;重新写提交记录

可是这样有个问题:

运行 hugo 静态文件都生成在了 public 文件夹, 当我切换到 master 分支, 静态文件文件还是都在 public, 而不是根目录, 因为我必须要把 public 内的文件直接推送到 master, github pages 才能识别进行部署, 这个时候就可以使用一个神奇的指令:

# 在项目根目录执行 将 master 分支作为 public 文件夹的工作区
git worktree add -B master public origin/master

之后每次 cd master 之后, 都会自动跳转到 master 分支, 这样就会很方便, 于是写个简单的部署博客脚本:

#!/bin/bash
hugo
cd public/
git switch master  # 现在这步没必要了
git add .
git commit -m "$(date)"
git push origin master

2. 一些错误#

这个连环错, 每次都遇到, 很离谱

➜  public git:(master) git push origin master
To github.com:jiyi27/jiyi27.github.io.git
 ! [rejected]            master -> master (non-fast-forward)
error: failed to push some refs to 'github.com:jiyi27/jiyi27.github.io.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details

➜  public git:(master) git pull origin master
From github.com:jiyi27/jiyi27.github.io
 * branch                master     -> FETCH_HEAD
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint:   git config pull.rebase false  # merge
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.

解决办法:

git pull origin master --no-rebase
  • 这会将远程的更改拉取到本地,并通过合并的方式整合

  • 如果没有冲突,Git 会自动完成合并

太晚了, 明天再研究这是怎么回事