Github: Mirroring gh-pages to master

16,434

Solution 1

git checkout gh-pages
git merge master
git push origin gh-pages

Solution 2

Add the following 2 lines to the [remote "origin"] section of .git/config:

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

Every time you push it will automatically push master to gh-pages as well. I'm using this for the jQuery Lifestream project.

Solution 3

Do not do what denbuzze suggests above!! The + (plus sign) in the push makes it quietly accept non-fastforward updates. I found out the hard way that this can irrevocably cause work to be lost by leading to dangling commits. Simply removing the plus signs makes this a safer approach.

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

now instead of causing a force update this will cause a warning & pull suggestion

To https://github.com/someuser/repo.git
 ! [rejected]        master -> gh-pages (fetch first)
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/someuser/repo.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Solution 4

I'm adding further explanation to @denbuzze and @MCSDWVL answers.

If you want to push both to master and gh-pages automatically each time you run git push origin, you probably want to add a Refspec to the git config of your repo.

So, according to the git-scm book, you can add two RefSpecs, by adding two push values to the repo config file .git/config:

[remote "origin"]
url = https://github.com/<github_user>/<repo_name>
      fetch = +refs/heads/*:refs/remotes/origin/*
      push = refs/heads/master:refs/heads/master
      push = refs/heads/master:refs/heads/gh-pages

That will cause a git push origin to:

  1. Push the local master branch to the remote master branch
  2. Push the local master branch to the remote gh-pages branch

by default.

Note: using a + before the spec causes to force push to the repo. Use it with caution:

The format of the refspec is an optional +, followed by <src>:<dst>, where <src> is the pattern for references on the remote side and <dst> is where those references will be written locally. The + tells Git to update the reference even if it isn’t a fast-forward.

Solution 5

I personally like to wrap this in an alias:

alias gpogh="git checkout gh-pages && git merge master && git push origin gh-pages && git checkout -"

This mirrors your master to gh-pages, pushes to github, then switches back the previous branch you were working on.

Share:
16,434
Ben Everard
Author by

Ben Everard

I'm Ben, co-founder and developer at The Idea Bureau.

Updated on July 14, 2022

Comments

  • Ben Everard
    Ben Everard almost 2 years

    I'm developing a jQuery plugin that's being hosting on GitHub. It has a demo included of which I'm manually copying and pushing to the branch gh-pages, what I'd like to do is have it so when I push a change to master it is automatically pushed to gh-pages, or at least a setup where they are mirrored.

    I've already seen this question but not sure if it really answers my question with regard to these requirements:

    1. I use Tower, I don't mind using the terminal (Mac) to make changes to config, so long as the solution works with this GUI.
    2. I only want this 'mirroring' on certain repos, not on all of them on my machine.

    Cheers

  • Ben Everard
    Ben Everard about 13 years
    Cheers Steve, I'll give that a go later and tell you how I get on.
  • Ben Everard
    Ben Everard almost 13 years
    Nice one Steve, the original solution didn't work but this works a treat.
  • christianvuerings
    christianvuerings over 12 years
    @James You're welcome, took a while before I found this one as well.
  • ThomasReggi
    ThomasReggi over 11 years
    Does everyone have to do this for each local repo? Or does this happy automatically when you clone it? (after you've committed with this change) Can you commit changes to .git/config?
  • ccleve
    ccleve over 11 years
    This is cool, except that this change doesn't get checked into the project, so everybody has the make the change by hand on their local boxes. It would be easy for it to get lost.
  • blong
    blong over 11 years
    Is there a typical way to share things like .git/config changes ?
  • christianvuerings
    christianvuerings about 11 years
    @b.long More and more people are using Dotfiles nowadays. dotfiles.github.com These contain useful shell scripts and good defaults
  • jjmerelo
    jjmerelo almost 11 years
    What that does is to push the same files onto the two branches, is that correct? Does that build the HTML from markdown, or just mirrors the changes in the master branch?
  • CWSpear
    CWSpear over 10 years
    This created a forces branch and pushed just to that branch when I tried this while using SourceTree, haha...
  • KyleMit
    KyleMit almost 10 years
    @denbuzze, is this true! Have you lied to us! :)
  • Sevin7
    Sevin7 over 9 years
    I'm not sure if what denbuzze suggests breaks anything because I didn't test it, but I do know that what you suggested works.
  • Sevin7
    Sevin7 over 9 years
    I tried this using github for windows (GUI client) and it doesn't work properly. It only commits to the first branch listed under push, and if the gh-pages is first then it auto updates the to merge from the gh-pages branch. I tried using SourceTree and it only commits to the first branch but it doesn't screw with the config file. Why doesn't this work with GUI clients? I don't want to have to open a command line to push and re-enter my credentials.
  • Dan Dascalescu
    Dan Dascalescu almost 9 years
    @MCSDWVL: why didn't you leave a comment on denbuzze's answer?
  • Dan Dascalescu
    Dan Dascalescu almost 9 years
    Will that push all the files from master into gh-pages, even those that have nothing to do with gh-pages? Why does GitHub make this so complicated? Wouldn't it be far simpler if it just rendered a folder gh-pages? Here's an attempt to create that kind of gh-pages folder setup.
  • Dan Dascalescu
    Dan Dascalescu almost 9 years
    Thoughts on @MCSDWVL's answer/comment below?
  • kthornbloom
    kthornbloom almost 9 years
    Just confirming what Sevin7 said. This doesn't work properly with the Github GUI on mac either. It updates the gh-pages branch, but the master is unaffected. Too bad, this wouldn't been nice!
  • HattrickNZ
    HattrickNZ over 8 years
    @Visgean Skeloru, so every time I push to the master, I then have to do this for gh-pages so master and gh-pages will be mirred.
  • Visgean Skeloru
    Visgean Skeloru over 8 years
    @HattrickNZ: yes but look at christianvuerings solution, that one is permanent and automatic...
  • Dan Dascalescu
    Dan Dascalescu over 7 years
  • Dan Dascalescu
    Dan Dascalescu over 7 years
  • Dan Dascalescu
    Dan Dascalescu over 7 years
  • Dan Dascalescu
    Dan Dascalescu over 7 years
  • Prashant
    Prashant over 4 years
    my config origin section has : fetch = +refs/heads/*:refs/remotes/origin/* url = git@something:user/repo.git as it is using * , do I still need to add both lines. I need my one branch to be synced with master.
  • Prashant
    Prashant over 4 years
    I have additionally in config : [branch "master"] remote = origin merge = refs/heads/master Do I still need to add first line to push to master in Origin section ?