Easily keep gh-pages in sync with master

I always loved Github’s ability to publish pages for a project and get the strain out of your server. However, every time I tried it, I struggled to keep the gh-pages branch up to date. Until I discovered the awesome git rebase.

Usually my github workflow is like this:

git add .
git status // to see what changes are going to be commited
git commit -m 'Some descriptive commit message'
git push origin master

Now, when I use gh-pages, there are only a few more commands that I have to use after the above:

git checkout gh-pages // go to the gh-pages branch
git rebase master // bring gh-pages up to date with master
git push origin gh-pages // commit the changes
git checkout master // return to the master branch

I know this is old news to some of you (I’m a github n00b, struggling with basic stuff, so my advice is probably for other n00bs), but if I had read this a few months ago, it would’ve saved me big hassles, so I’m writing it for the others out there that are like me a few months ago.

Now if only I find an easy way to automate this… 🙂

  • http://twitter.com/mrtazz Daniel

    Just curious, is there a specific reason you use rebase instead of doing `git checkout gh-pages && git merge master`?

    • http://leaverou.me Lea Verou

      Merge scares the shit out of me 😛 No other reason.

      • Anonymous

        Fair enough :).

    • http://twitter.com/MarcoBarbosa Marco Barbosa

      Also rebase keeps the commits history 🙂

      I think Github recommends rebase vs merge, actually.

      • Anonymous

        It’s actually the opposite. Rebase rewrites the commit history to have branch-local commits on top of commits you rebase onto. That’s why you should never rebase commits you have already pushed.

    • http://twitter.com/desandro Dave DeSandro

      +1 for this. I use `git merge master` to sync.  Rebase scares me.  I discuss a bit about master & gh-pages branches in http://metafizzy.co/blog/one-docs-to-rule-them-all/

  • http://mathiasbynens.be/ Mathias Bynens

    I’m a Git noob myself, but I’m pretty sure that instead of `git push origin master` you can just use `git push` (assuming the `master` branch is available).

    You could automate all those commands things using a post-commit hook.

    • http://leaverou.me Lea Verou

      Thanks, I’ll try that next time!

      • http://twitter.com/philippbosch philippbosch

        Here’s a simple post-commit hook that should help with that: http://get.inject.io/n/XxsZ6RE7

    • dalzuga

      sometimes `git push` doesn’t work and you have to `git push origin master`

  • http://twitter.com/necolas Nicolas Gallagher

    Sometimes you may just want to keep a specific set of files in sync across branches. For example, the master branch may include a couple of  scripts and a readme, but the gh-pages branch would need additional files that are involved in the presentation and construction of a project page.

    To keep the scripts in the gh-pages branch up-to-date with those in master, rather than doing the `git rebase master` step, you can use `git checkout master — filename.ext anotherone.ext` and then commit those changes. That can help you to keep your master branch free of files that are only needed for the gh-pages branch.

  • http://profiles.google.com/ionel.mc Ionel Maries Cristian

    But why would you store code in the gh-pages branch? That’s meant for static html …

    • http://leaverou.me Lea Verou

      Usually you want to demo the script on the project page, so it needs to be there as well. 

      I can see what you mean with server-side code, but all my repos are JS-related.

      • Anonymous

        I have the same scenario (all repos are JS). I was curious: other than it not being the default branch, would there be any disadvantage to using gh-pages AS your master? i.e. not having a master branch at all.

        Any updated recommendation on a workflow for this?

        • http://leaverou.me Lea Verou

          Yup, I’ve done this for css3test. See: http://oli.jp/2011/github-pages-workflow/

        • Anonymous

           Yeah, that’s sound like the best option.  I’ll pursue the same.  Thanks for the update!

    • http://twitter.com/necolas Nicolas Gallagher

      See my comment below about syncing specific files between master and gh-pages: http://leaverou.me/2011/10/easily-keep-gh-pages-in-sync-with-master/#comment-333642092

      • Owen Densmore

        This is still a big deal issue for me. And yes, my master repo is all needed in the gh-pages.

        So love the brilliant conversation here and in its transitive closure.

  • Peter van der Zee

    Heh, I was asking around for the same thing (how to automate the syncinc) on twitter during Fronteers. Did not get an anwer. I tried removing the master branch but that didn’t seem to go too well with GitHub. The best solution would be for GitHub to allow us to change the “pages” branch, defaulting it to gh-pages like it is now. Not sure if that’ll happen though.

    I did find out that you can have something like this in your config. Of course that means that it’s a local setting thing. But if you don’t work on git from many computers that might be a solution for you 🙂

  • http://twitter.com/yodirkx Rudie Dirkx

    Too bad GIT doesn’t have a post-push hook. Reason: that kind of hook wouldn’t add functionality, only ease. That’s not good enough =)

  • http://mineshafter.appspot.com/ download13

    My solution for this problem has so far been to simply not create gh-pages demos for my projects.
    I like this solution better.

  • http://paulirish.com Paul Irish

    Oli detailed a few other approaches here: http://oli.jp/2011/github-pages-workflow/

  • http://thatryan.com thatryan

    Any idea why when I run git rebase master I get an error saying 

         “It seems that I cannot create a rebase-apply directory, andI wonder if you are in the middle of patch application or anotherrebase..” 

  • Pingback: 実践GitHub Pages運用のユースケースとワークフローの詳細 | ゆっくりと…()

  • http://vstarkov.com/ Vladimir Starkov

    thank you!

  • Andy Grohe

    This only seems to work if the directory structure of gh-pages and master are the same, right? In my case, the gh-pages directory only contains the doc sub-dir of the master.

    I have to copy the files out of the master doc dir, switch to gh-pages, copy those back in, check them in, switch back to master.

    Would love to know a better way

  • Pingback: Github: From Zero to Hero | John Polacek()

  • http://rachelnabors.com Rachel Nabors

    DID you ever find a way to automate this?

  • http://kizi-friv.kizi2.com/ kizi friv

    That’s very helpful information..thanks for sharing it to us.I like it very much,I’ll get your myspace.

  • Pingback: Open Source Your Code For Fame and Fortune | John PolacekJohn Polacek()

  • http://www.martinhansen.no Martin Hansen

    I use grunt-gh-pages to automate this process. Setup a task to build your code, docs, examples or whatever. Typically have all those files in dist, then run grunt gh-pages and it checks out a repo copy in a subdir. Checks out branch gh-pages, copies files, commits and pushes. Timesaver.

  • paul_verest
  • Pingback: Links for 10th January | Web Developer Essentials()

  • CWSpear

    I found probably the easiest/quickest solution here: http://stackoverflow.com/a/7472481/592839

    Which says to add the following to the [remote "origin"] section of .git/config:

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

    and whenever you push to origin, it pushes the code on your local master branch to both remote branches.

    • Matt

      I know this is an old topic but I found it on google. If anyone is reading DO NOT DO WHAT THIS POST SAYS! THE + at the start of the right hand side means FORCE PUSH, if anyone else commits between your pushes you will destroy all their commits and not even leave them in the repo to roll back to!

      • dalzuga

        As good practice, don’t do anything you don’t understand. Basically this means that you should not edit any .git stuff unless you really know what you’re doing. Use git commands instead. Thanks for this post.

  • Pingback: מדריך למתחיל : GitHub Pages | WWWEBBIE()

  • ashes999

    After reading all the comments, I, too, went with a gh-pages only solution. Do you mind updating your post to add a note at the bottom or something? I almost missed this and went with a manual synchronization process.

  • Sanket Sahu

    For specific files or folders, just use
    git checkout master — myplugin.js

    Instead of
    git rebase master

  • Pingback: Wuthering Bytes | The Beth Project()

  • Pingback: Easily keep gh-pages in sync with master | Lea Verou | Business Intelligence Survival Kit()

  • Pingback: How to host a static site with Github Pages - Henry Bayuzick()

  • Saad Quadri

    Thanks, this helped me quite a bit!

  • http://www.lucianooliveira.net Luciano Oliveira

    Thank you for the tip, Lea!

  • Pingback: Where to place the gh-pages folder? | Octopus in vitro()

  • Pingback: Additional Github notes | Stone Knives and Bear Skins()

  • Anna Ivanova

    this was perfect, thanks!

  • Alison

    Wow thank you! The ‘git rebase master’ step was exactly what I needed but I wouldn’t have been able to figure that out on my own!

  • Pingback: Commit Git to Memory - Danielle()

  • http://www.pazams.com/ maor pazams

    Consider using an orphan branch, see http://www.pazams.com/Github-syncing-the-gh-pages-branch/

  • http://www.emrealadag.com/ Ahmet Emre Aladağ

    I clone the same repo twice: blog (master branch) and blog-html (gh-pages branch). Then I generate the HTML files into ../blog-html. That solves the problem.

  • Qualia Reed Baker

    I’m just getting started with Github, so I apologize if this is obvious already. Is this function to take the information on gh-pages and push it to the master or to take the information on the master and push it to gh-pages? I have been trying to figure out how to get all my code on gh-pages onto the master and I’ve been finding the phrasing a bit ambiguous.

    • dalzuga

      From master to gh-pages

  • anaagustanti

    tips to keep kesinkronan gh-pages and master useful for jual crystal x asli

  • Pingback: Keep gh-pages and master in sync with one line of code - John Polacek - Chicago Web Developer()

  • jpolacek

    Just published a quick how-to for turning this into a one-liner using Git Aliases: http://johnpolacek.com/2016/01/29/keep-gh-pages-and-master-in-sync-with-one-line-of-code-2/

  • febriana

    tips synchronize gh-pages and master more or less understand jual crystal x di yogya

  • nurdin

    angel banget arep nglebokake crystal x asli

  • Pingback: Easily keep gh-pages in sync with master | Lea Verou | Ade Viankakrisna Fadlil()

  • Pingback: rialudi()

  • Pingback: знакомства с Украинскими девушками()

  • Pingback: zootopia 2016()

  • Pingback: film online 2016 free ua ru kz()

  • Pingback: Дивергент, глава 3: За стеной / Allegiant, Высотка / High-Rise,()

  • Pingback: лучшие фильмы 2016 2016 года список новые фильмы()

  • Pingback: лучшие фильмы года 2016()

  • Pingback: jykfqy abkmvs cvjnhtnm ,tp htubcnhfwbb()

  • Pingback: more()

  • Pingback: film()

  • Pingback: ukraine girl kiev lviv()

  • Pingback: ukraine girl kiev lviv 2016()

  • Pingback: helou zis me()

  • Pingback: helou zis me()

  • Pingback: clic here()

  • Pingback: clic()

  • Pingback: illuziaobmana()

  • starbeamrainbowlabs

    You could automate this with a makefile. Example: http://hastebin.com/omokomisex

  • Pingback: HDKINOONLINE()