How to properly commit in repository with Github Pages branch

懵懂的女人 提交于 2019-11-27 09:34:05

I have a pair of scripts that do exactly this. In my situation, doc sources are in . (relative to where the scripts are run, of course), generated documentation is in _build/html, and I issue the commands

touch _build/html/.nojekyll    # disable GitHub's Jekyll page generator
./commit-to-gh-pages.sh _build/html

where commit-to-gh-pages.sh is

#! /bin/sh

# Commit the generated HTML pages to the branch gh-pages.
# Will not push them to GitHub.

set -e -v

treehash=$(./hash-tree.py "${1:-_build/html}")
parent=$(git rev-parse gh-pages)

msg="Regenerated docs for $(git rev-parse HEAD)"
commithash=$(echo "$msg" | git commit-tree $treehash -p $parent)
echo "Updating gh-pages to $commithash"
git update-ref refs/heads/gh-pages "$commithash"

This writes the directory _build/html to the Git index, makes a commit object that has exactly this directory tree as its contents, and writes the commit object to the gh-pages branch with the previous state of that branch as the parent.

The tricky part is the hash-tree.py script, which extends the git hash-object command to directory trees. I won't copy-paste it, but you can get it here. (If anyone knows a more elegant way to do this, please tell me.)

I have had excellent experience with cloning the repo into a subdirectory of itself and checking out a different branch: For your case something like

# add public to .gitignore
git clone . public
cd public
git checkout --orphan gh-pages
git reset --hard
git commit --allow-empty "initial"

gets you started. See http://krlmlr.github.io/git-subbranch for a writeup.

You could try using git subtree if you have git version > 1.7.11. I can barely manage to understand the details myself yet, so I won't try to explain it, but there is a tutorial at Hugo website builder. The relevant part starts at the heading configure git workflow.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!