That time I deleted master on purpose...

git push origin :master is a pretty scary command to run. Let me explain though. I decided to use a static site generator called Assemble. The Grunt plugins are all tailored to releasing the compiled static site from a sub-folder of the project to the branch gh-pages. This is perfect if you are using Github Pages for a project but what about if you are using it for your organization? The key difference here is that Github looks at the master branch for an organization instead.

So the solution here was to do some branch gymnastics.

  • git checkout -b build
  • Using the fantastic grunt-gh-pages plugin we tell it to use master instead:
     'gh-pages': {
        options: {
          base: 'site',
          branch: 'master'
        },
        src: ['**']
      }

but we get errors:

hank@hank1:~/sites/hankyates.github.io$ grunt gh-pages  
Running "gh-pages:src" (gh-pages) task  
Cloning git@github.com:hankyates/hankyates.github.io.git into .grunt/grunt-gh-pages/gh-pages/src  
Cleaning  
Fetching origin  
Checking out origin/master  
Removing files  
Copying files  
Adding all  
Committing  
Pushing  
Warning: To git@github.com:hankyates/hankyates.github.io.git  
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:hankyates/hankyates.github.io.git'  
To prevent you from losing history, non-fast-forward updates were rejected  
Merge the remote changes (e.g. 'git pull') before pushing again.  See the  
'Note about fast-forwards' section of 'git push --help' for details.  
 Use --force to continue.

Oh no! git doesn't like that we are trying to push an orphaned branch with no history to a branch with history. Normally this is a good thing. Git has prevented us from clobbering our history, but we still have our history and our code but it's on the build branch instead now.

It's at this point we run the super dangerous command git push origin :master which deletes the master branch on github. Note that the grunt plugin clones the repository to a temporary folder, copies the compiled static site, commits, and pushes to master. So we still have a copy of master locally and can push it back if need be.

After running grunt gh-pages our site is is compiled and pushed to Github. A nice trick is to set the default branch on Github to build.