Fix for '[rejected] gh-pages -> gh-pages (non-fast-forward)' in GitHub
TL;DR
git config --global push.default tracking
Full Story
GitHub Pages are public webpages freely hosted and easily published through github.com. They are most commonly used to create good-looking documentation for code hosted on GitHub (case in point: http://npr.github.com/responsiveiframe/). GitHub Pages have also become a popular solution for hosting minimalistic websites and/or blogs. That includes the blog you are reading: Fresh Blurbs is hosted on GitHub Pages.
You can manage Github Pages through a web user interface, but often you may desire full control over the end result, consequently going down the manual setup route.
When you set GitHub Pages up manually, you have to create an “orphaned” branch called gh-pages
. This is
the branch where your Jekyll templates or static HTML will reside.
For any repository GitHub also creates a master
branch in your repo. For code pages, master branch usually hosts
the code part of the project. For websites and blogs master
branch may be empty or the place where you
keep Jekyll source (if you use custom plugins and have to pre-build site before pushing to github.com).
To make the long story short, when using GitHub Pages you typically end up with at least two branches:
master
and gh-pages
. The two branches are not “branches” of each other in the classical version-management
sense: they were never intended to be merged or have anything in common.
This can create certain issues.
If you manually manage GitHub pages you most probably have gh-pages
and master
checked out in
separate folders, pulling and pushing the two from and to github.com independently. I would also guess
that when pushing you are frequently annoyed by an error message that looks something like:
! [rejected] gh-pages -> gh-pages (non-fast-forward)
or this (depending which workspace you are pushing from):
! [rejected] master -> master (non-fast-forward)
Here is why you are getting it: in each workspace Git tries to push all existing branches to GitHub, not just the current one. Given how you have two independent branches, things frequently go awry.
To solve the problem you can modify the default behavior. Typically, you don’t really need Git to push all branches, just the one you are working on. So, why not tell Git that is what you want? Here is how you do it:
git config --global push.default tracking
Easy peasy, no more annoying messages and we are back to being in love with Git (because it rocks).