Skip to content

Host on GitHub

Sergii Gatezh edited this page Apr 29, 2019 · 2 revisions

Hugo docs

You can also tell GitHub pages to treat your master branch as the published site or point to a separate gh-pages branch. The latter approach is a bit more complex but has some advantages:

  • It keeps your source and generated website in different branches and therefore maintains version control history for both.
  • Unlike the preceding docs/ option, it uses the default public folder.

Preparations for gh-pages Branch

These steps only need to be done once. Replace upstream with the name of your remote; e.g., origin:

Add the public Folder

First, add the public folder to your .gitignore file at the project root so that the directory is ignored on the master branch:

echo "public" >> .gitignore
Initialize Your gh-pages Branch

You can now initialize your gh-pages branch as an empty orphan branch:

git checkout --orphan gh-pages
git reset --hard
git commit --allow-empty -m "Initializing gh-pages branch"
git push upstream gh-pages
git checkout master

Build and Deployment

Now check out the gh-pages branch into your public folder using git’s worktree feature. Essentially, the worktree allows you to have multiple branches of the same local repository to be checked out in different directories:

rm -rf public
git worktree add -B gh-pages public upstream/gh-pages

Regenerate the site using the hugo command and commit the generated files on the gh-pages branch:

commit-gh-pages-files.sh

hugo
cd public && git add --all && git commit -m "Publishing to gh-pages" && cd ..

If the changes in your local gh-pages branch look alright, push them to the remote repo:

git push upstream gh-pages
Set gh-pages as Your Publish Branch

In order to use your gh-pages branch as your publishing branch, you’ll need to configure the repository within the GitHub UI. This will likely happen automatically once GitHub realizes you’ve created this branch. You can also set the branch manually from within your GitHub project:

  1. Go to SettingsGitHub Pages
  2. From Source, select “gh-pages branch” and then Save. If the option isn’t enabled, you likely have not created the branch yet OR you have not pushed the branch from your local machine to the hosted repository on GitHub.

After a short while, you’ll see the updated contents on your GitHub Pages site.

Put it Into a Script

To automate these steps, you can create a script with the following contents:

publish_to_ghpages.sh

#!/bin/sh

DIR=$(dirname "$0")

cd $DIR/..

if [[ $(git status -s) ]]
then
    echo "The working directory is dirty. Please commit any pending changes."
    exit 1;
fi

echo "Deleting old publication"
rm -rf public
mkdir public
git worktree prune
rm -rf .git/worktrees/public/

echo "Checking out gh-pages branch into public"
git worktree add -B gh-pages public upstream/gh-pages

echo "Removing existing files"
rm -rf public/*

echo "Generating site"
hugo

echo "Updating gh-pages branch"
cd public && git add --all && git commit -m "Publishing to gh-pages (publish.sh)"

This will abort if there are pending changes in the working directory and also makes sure that all previously existing output files are removed. Adjust the script to taste, e.g. to include the final push to the remote repository if you don’t need to take a look at the gh-pages branch before pushing. Or adding echo yourdomainname.com >> CNAME if you set up for your gh-pages to use customize domain.