Create a gist now

Instantly share code, notes, and snippets.

Setup GitHub Pages "gh-pages" branch and "master" branch as subfolders of a parent project folder ("grandmaster").


Description: Setup GitHub Pages "gh-pages" branch and "master" branch as subfolders of a parent project folder ("grandmaster").

Author: Chris Jacob @_chrisjacob

Tutorial (Gist):

The Result

The final folder structure on my local system is:

/grandmaster/master/.git # checkout of "master" branch
/grandmaster/gh-pages/.git # checkout of "gh-pages" branch (removed "master" branch)

See "master" branch:

See "gh-pages" branch:

See GitHub Page (auto generated):

The Process

A note for GitHub novices - replace "chrisjacob" with your own GitHub username.

A note for Terminal novices - you don't need to enter the "ichris:Sites $ " parts of the code listed below. ^_^

Visit GitHub and create a new repository with the project name "grandmaster".

Don't follow GitHub's Next steps instructions! Follow the steps below to setup your projects folders on your local system.

Open, create project parent folder "grandmaster", and a subfolder for the "master" branch. Initialise a new git repository for the project and push the "master" branch to GitHub.

ichris:Sites $ mkdir grandmaster
ichris:Sites $ cd grandmaster/
ichris:grandmaster $ mkdir master
ichris:grandmaster $ cd master/
ichris:master $ git init
ichris:master $ echo "# Master README file" > README.markdown
ichris:master $ git add .
ichris:master $ git commit -m "Master README added"
ichris:master $ git remote add origin
ichris:master $ git push origin master

Refresh your projects "master" branch page on GitHub to see the committed files.

Auto generate a GitHub Pages branch, with some default content.

Or follow these steps to get to the generator page:

  1. Go to the projects Admin page on GitHub

  2. Check the "GitHub Pages" checkbox

  3. A popup will ask you to "Activate GitHub Pages" - click the big "Automatic GitHub Page Generator" button

Check that your GitHub Pages page has been built and is available.

Back in, change directory back to the parent folder, setup a "gh-pages" subfolder for your "gh-pages" branch and change directory into it.

ichris:master $ cd ../
ichris:grandmaster $ mkdir gh-pages
ichris:grandmaster $ cd gh-pages/

Clone your "grandmaster" repository into the "gh-pages" folder (this will clone in the "master" branch), checkout the "gh-pages" branch, list the files (should have "index.html" and ".git") and then remove the "master" branch to avoid any confusion. Last step is to check that "master" branch was removed and only "gh-pages" branch is listed.

ichris:gh-pages $ git clone .
ichris:gh-pages $ git checkout origin/gh-pages -b gh-pages
ichris:gh-pages $ ls -la
ichris:gh-pages $ git branch -d master
ichris:gh-pages $ git branch

You will probably get a warning when deleting the "master" branch... don't worry about it ^_^

Lets add a "README.textile" file to the "gh-pages" branch

ichris:gh-pages $ echo "h1. GitHub Pages README file" > README.textile
ichris:gh-pages $ git add .
ichris:gh-pages $ git commit -m "Child README added"

Now push to the "gh-pages" branch

ichris:gh-pages $ git push origin gh-pages

Visit your projects "gh-pages" branch page on GitHub to see the committed files.

If everything has gone well you now have a parent project folder named "grandmaster", with subfolders for its two branches "master" and "gh-pages"; each containing a checkout of their respective branch.

For me this system keeps things nice and tidy without needing to do git checkout gh-pages each time I want to view my "gh-pages" branch.

Might also be a useful structure for output from static site generators like Jekyll, Webby, or nanoc.

Enjoy ^_^

trans commented Oct 26, 2011

Thank you. Thank you! THANK YOU! I've been cursing gh-pages for over a year and had to fuss with things like grancher, it's failure on 1.9, then writing my own task to copy the files, which had it's own edge cases. Now I am free!!!

FYI. I did one thing different, I just made the gh-pages directory a subdirectory of my main project and added it to .gitignore.


You're most welcome. Thank you for the feedback. I also played the 1.9 dance with Grancher ;-)

RE: gh-pages as a subfolder for master, I tried that too, but found the setup fails when switching between branches, see the note I made here:

trans commented Oct 26, 2011

Hmm... how do you loose it? I just tired it and it is still there. Anything in .gitignore just hangs about no mater what branch you are in.


Ahhh k. I never tried with the gh-pages folder in .gitignore - I will have to give it a go, but as you say, sounds like it works fine. Thx for the tip ^_^

trans commented Oct 26, 2011

Nothing doing. I can't even describe how tickled pink I am that you set me down the "right path" on this.

Also, it just occurred to me that one could also do:

$ cd myapp
$ git clone --local . gh-pages

But I'm not yet sure how that plays out when pushing gh-pages to the remote.

trans commented Oct 26, 2011

Ok. I tried out local clone and I think it's not a good way b/c it means pushing twice. Once from gh-pages to local and again from master to remote gh-pages.


Hello, thanks for sharing this.
Someone know how to keep some files in gh-pages sync with the master branch (like in this article with this directory architecture?
Seems not possible because master branch is cloned then deleted in gh-pages folder, and checkout can't point to a specific external folder (at least I dunno how to do)...

trans commented Mar 13, 2012

I just use a rake task, or such device, to keep files in sync between the two when need be.



If you've already followed this gist then:

  1. cd /grandmaster/gh-pages/
  2. git checkout origin/master -b master
  3. git checkout gh-pages
  4. git checkout master -- README.markdown

You should now have "README.markdown" and "README.textile" in the gh-pages folder.

... Also you don't need to delete the master branch inside the gh-pages folder. So if you need to pull in files from "master" like Nicolas's post outline then certainly keep "master" around - p.s. Thanks for the link to this by the way - great tip! ;-)


hello everyone, when we deploy pages on github branch, we can access those pages using
But I dont want the username to appear in the link, instead I want it to be any other name, is that possible?


I followed theses instructions and it worked fine. Thank you, I managed to put my Doxygen files in my gh-pages.


Hi, I know this is old, but I'm trying to do something similar, with this twist: I want my gh-pages branch to be the same as a sub-directory of my master branch. So in my master, I have:

node-server.js // a very simple node server for local development
      // psd files, wireframes, etc.
    index.html, etc. // the real project -- a static site that I also want to serve from gh-pages

I've successfully created a gh-branch, and manually copied the stuff from site/ to it, but do you have any advice on how to easily keep them in sync? Can I do something like:

grandmaster$ ln-s master/site gh-pages

@sprugman I'd like to have a similar setup 👍


@chrisjacob, you're awesome!


Hey, very nice tutorial.

Since i'm new to GitHub, I was using their tutorial to create a fork. However, I prefer your approach since I like to keep things organised. But I'm not sure if I got it right. At Github article they show us how to keep your fork updated with the original one, doing this:

cd Spoon-Knife
# Changes the active directory in the prompt to the newly cloned "Spoon-Knife" directory
git remote add upstream
# Assigns the original repository to a remote called "upstream"
git fetch upstream
# Pulls in changes not present in your local repository, without modifying your files

But that is only for the 'master' branch, right? Using your approach, how can I have both master and gh-pages in sync with the original repo?



So basically all you're doing is creating two separate working copies on different branches? In no way are the working copies linked, aside from having the same remote?

dideler commented Sep 13, 2013

I stumbled on this and at first had no idea why you would want to do this, then you mentioned it being useful for output from static site generators. With this I can probably use a single repo instead of two repos (one for the documentation's source which uses Sphinx, the other for gh-pages). Thanks!

BTW, the webpage 404s:

dideler commented Sep 13, 2013

Has anyone tried to replicate this with git submodules?

krlmlr commented Dec 4, 2013

I have written a small script that assists in synchronizing a subdirectory with the gh-pages branch. It's in my scriptlets repo. Use ghpsd init for creating an empty gh-pages subdirectory that will hold the contents of your gh-pages branch. After adding to this subdirectory, use ghpsd merge for updating the gh-pages branch. Note that you still have to push to GitHub.

It works by cloning a copy of the repo into a shadow subdirectory named .gh-pages (which is added to .gitignore, too); this makes updating the gh-pages branch work seamless. Call ghpsd checkout to recreate the hidden .gh-pages folder, this clones locally and does not require network access.


Hi! Very nice tutorial. I have a question: how can I create the two repositories via Github web interface and then do what your tutorial says (I don't want to use automatic page generator)?
Thanks in advance!


use symbolic link for _site to gh-pages, and when you build the site ,it will auto-update the gh-pages.

alias blog0='~/.gem/ruby/2.1.0/bin/jekyll build'
alias blog99='git add . && git commit -a -m "update site" && git push'
alias blog.username='blog0 && cd .. && cd gh-pages && blog99 && cd .. && cd master'

I'm very new at this, and your instructions didn't at first work for me. When I tried to push, I got an error message: Permission denied (publickey). A web search led to me setting up the necessary keys for the SSH stuff to be in place. And then your instructions worked until this line:

ichris:gh-pages $ git checkout origin/gh-pages -b gh-pages

When I tried that, I got an error message:

 fatal: not a git repository (or any of the parent directories): .git

Any ideas what I may have missed??

Keav commented Sep 16, 2014

@rafaelespericueta I had the same issues!

Instead of running git remote add origin I had to run git remote set-url origin

When trying to checkout gh-pages, I found that in the previous step to clone the repo, it had cloned the whole repo under gh-pages - creating the folder structure:


Manually moving all the files and folders (including the .git folder) up the tree one level (into gh-pages) and deleting the now empty extraneous "grandmaster" folder got everything back on track.

Keav commented Sep 16, 2014

Further to my post above - at the clone step, use:

git clone -b gh-pages --single-branch .

Note the "." at the end!

This will clone only the gh-pages branch (no need to do the delete master branch step) and also place it correctly without creating a further project folder.

koppor commented Nov 23, 2015

With git 2.5, you don't need two clones. Just use git worktree add -b gh-pages ../gh-pages origin/gh-pages as described at


how to make an existing project to become just like in this format? I have an existing project in github and would like to have a local storage formatted like this but this guide seems to help build the format from zero. What steps should I do to make it?


how to make something like this for an existing repository?

hendrawd commented Jun 14, 2016 edited

i got this error

fatal: Cannot update paths and switch to branch 'gh-pages' at the same time.
Did you intend to checkout 'version-control/grandmaster/gh-pages/origin/gh-pages' which can not be resolved as commit?

when try to
git checkout origin/gh-pages -b gh-pages


hmm, well i somehow able to achieve the same result with different commands

sfscs commented Jun 16, 2016

The essence of this gist answers the question "how do I alter two branches without having to checkout constantly between them?"

For an existing repo you can actually skip most of the instructions in this gist since most of them are a sort of complicated way of creating the repo as you go.

So if you already have a repo on github, follow these instructions to create the branch locally and push it back to github:

Once you have the branch made, clone the repo locally into a sibling folder with a different name. In this sibling folder checkout the gh-pages repo, then delete the master branch. This way you can push to master from one folder and push to gh-pages from the other but you can see the files side by side.


I really like this approach. Thanks for the tip.


Thanks for the post. It works for my pages

git push --force origin $(git commit-tree -m "auto" master:dist):gh-pages
tree="$(cp .git/index{,-bk} && git add -f dist && git write-tree --prefix=dist && mv .git/index{-bk,})"

git push -f origin "$(git commit-tree -m auto "$tree")":gh-pages
tree="$(export GIT_INDEX_FILE="$(mktemp)"; cat .git/index >"$GIT_INDEX_FILE"; git add -f dist && git write-tree --prefix=dist)"
    after that, imo you should do something like 
    git update-ref refs/heads/gh-pages "$(git commit-tree -p gh-pages -m auto "$tree")"

still trying to figure it out with a build folder that is in gitignore but would be root level on gh-pages deploy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment