One of the great features of GitHub is that it allows you to host static content. This post looks at how this blog is published to GitHub.

Using this article as a guide and this one:

Setup

Basically what we will do is commit the source code into a source branch and have the compiled code on the master branch:

1
2
3
4
# rename current master to something else
git branch -m master master-old
# Create source branch from the old master and check it out
git checkout master-old && git checkout -b source

Publishing Steps

  1. Commit your new content to the source branch!!!
  2. Build your site from the source branch into /web (your output folder). I’m using grunt to do this step.
  3. Checkout the master branch
  4. Remove the existing content files from master
  5. Copy the new site content into the root project directory
  6. Add the new site content to git & push to origin

Here’s a script to do Step 3 onwards, which I call from grunt after doing step 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
git status
# Checkout master branch
git checkout master
# Rename the /web directory to .web
mv web .web
mv node_modules .node_modules
# Remove all non . files
rm -rf *
# Copy everything from .web to current dir
rsync -a .web/ ./
# Remove .web
rm -rf .web
# Add everything
git add .
# Commit using the same commit message as from the 'source' branch
git commit -C source
git push origin master
# Remove everything again
rm -rf *
# Re-add node_modules
mv .node_modules node_modules
# checkout source again
git checkout source
# undo any pending changes to any files
git checkout -- .
git status