Something I recently ran into: How do you build a site locally and host the resulting built version of the site on GitHub Pages? Here’s one way to do it. First, have a make all target that builds the public files from the source pages (for example, pass CommonMark filenames to Pandoc to get HTML.) Then make deploy does several things:

  1. Make a fresh build directory and copy the source files, the Makefile, and the .git directory into it.
  2. Run make gh-pages inside the build directory. That target depends on the all target, so this builds the site inside the directory.
  3. Next, make gh-pages inside the build directory commits the public files on the gh-pages branch, then force pushes to GitHub.
  4. Back in make deploy, delete the build directory. That includes the .git directory inside, so no history of built files gets preserved.
# We make this site with "make" locally and deploy generated pages to GitHub in
# a branch. First, delete the build directory and the gh-pages branch. Then
# copy the site files into the build directory and make the gh-pages target
deploy : all
    (git branch -D gh-pages || true) &> /dev/null
    rm -rf build && mkdir -p build
    cp -a Makefile .git $(SOURCES) build
    make -C build gh-pages
    rm -rf build

# This target only runs inside the build directory and does a commit and push
# on the gh-pages branch. If you look at this project on GitHub you should see
# the original .md files on the main branch and the generated HTML files on the
# gh-pages branch.
gh-pages : all
    basename `pwd` | grep -q build || exit 1
    rm -f .git/hooks/pre-push
    git checkout -b gh-pages
    git rm -f $(SOURCES)
    git add -f $(PUBLICFILES)
    git commit -m "this is a temporary branch, do not commit here."
    git push -f origin gh-pages:gh-pages

This is from a Makefile for a very basic .org site. The -C option to make is to change to the given directory before reading the Makefile. (That site has versioning for the source files on GitHub too, but it doesn’t have to be.)

Another option is to put the built files in a subdirectory, but then you end up committing the built files on the main branch: Using different static site generators · community · Discussion #21563