On the occasion that you’re working on a web-related project using GitHub, you may want to simply mirror your demo pages or entire master branch over to the Github Pages for your project to make them web-accessible. An example case for this would be the development of a JavaScript tool that includes an “examples/” section. It would be most convenient to just let people browse that as a webpage from GitHub, keeping it up to date every time the master branch changes.

GitHub Pages make it possible to serve web content from a branch of your repository, and the automatic page generator is very nice. To make it work in the example above, though, an update to both the base repository and the website requires at least:

git checkout master
git status
git commit -am "Committing changes to master"
git push origin master
git checkout gh-pages
git rebase master # or merge, whatever your preference
git push origin gh-pages
git checkout master

While workflows vary (and some of those commands can be shortened), most require a similar procedure. That’s a lot of work. Guess what? Thanks to a tip from denbuzze on Stack Overflow I found a way to keep gh-pages in sync with master that’s so simple you’ll forget it’s even happening.

In your local clone of your GitHub repository, add these two lines to .git/config, in the [remote “origin”] section:

push = +refs/heads/master:refs/heads/gh-pages
push = +refs/heads/master:refs/heads/master

The section should now look something like this:

[remote "origin"]
	fetch = +refs/heads/*:refs/remotes/origin/*
	url = git@github.com:user/repo.git
	push = +refs/heads/master:refs/heads/gh-pages
	push = +refs/heads/master:refs/heads/master

Now, when you push to GitHub (git push), your gh-pages branch is mirrored and everything in your repository is viewable at http://user.github.com/repository/. That’s pretty sweet.