My set up
For this site I’ll be using Hugo, a static site generator that I’ve been using for several projects at the moment. Because in my search for a good and easy solution to host on Github pages I didn’t find a good source of information, I will start off with giving a short guide on how I set up my site.
Assuming that you have set up your website using the guide on the Hugo website you will have a folder with the source of your website. To deploy to Github Pages you will need to create a repository that has the index.html file at the root on the master branch. Unfortanetly Hugo builds the pages into a folder named public. To overcome this, I have a solution based on this approach. I keep the source for the website in my local repository on a develop branch, and the build on the master.
Github Setup
After creating the repository on Github, add a .gitignore file to the master branch that looks like this
themes/
public/
This will make sure the build of the website and the theme being stays untracked in the Git repository.
Local Setup
Then locally, initialize a Git repository at the root of your site and checkout a develop branch immediately. Commit your files and add Github as a remote.
git init
# create new branch called develop and switch to it.
git checkout -b develop
# track all the source files for our site.
git add .
# make our first commit
git commit -m "initial commit."
# and add the GitHub repository as a remote.
git remote add origin <URL to your GitHub pages repository>
Deploying
To deploy create a file deploy.sh and make it executable. This script will build the website into the public folder and then checkout to the master branch. Since the public folder is untracked we can use this to copy everything to the root, commit that to the master branch and push that to the Github remote. Once that is done we have succesfully deployed, we can now checkout the develop branch again and delete the master branch locally.
#!/bin/bash
# Temporarily store uncommited changes
git stash
# Verify correct branch
git checkout develop
# Build new files
hugo
# Get previous files
git fetch --all
git checkout -b master --track origin/master
# Overwrite existing files with new files
cp -a public/. .
# Commit
git add -A
git commit -m "Publish."
# Push
git push origin master:master
# Restoration
git checkout develop
git branch -D master
git stash pop
Keep source private
Finally I host the actual source of my website on a private Bitbucket account by setting your upstream for the develop branch to the Bitbucket repository.
# Add bitbucket as a remote
git remote add bitbucket https://bitbucket.com/user/repo.git
# Push and set upstream for remote branch to bitbucket repo
git push --set-upstream bitbucket develop