How to properly commit in repository with Github Pages branch

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.)

Leave a Comment