68

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

1

8 Answers 8

108

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.

11
  • @James You're welcome, took a while before I found this one as well. Nov 8, 2011 at 12:48
  • 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? Sep 28, 2012 at 21:22
  • 4
    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.
    – ccleve
    Oct 15, 2012 at 20:09
  • 1
    Is there a typical way to share things like .git/config changes ?
    – blong
    Dec 21, 2012 at 2:58
  • 1
    @b.long More and more people are using Dotfiles nowadays. dotfiles.github.com These contain useful shell scripts and good defaults Mar 30, 2013 at 2:44
67
git checkout gh-pages
git merge master
git push origin gh-pages
5
28

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.
5
  • @denbuzze, is this true! Have you lied to us! :)
    – KyleMit
    Jun 19, 2014 at 6:07
  • 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
    Nov 28, 2014 at 14:41
  • 3
    @MCSDWVL: why didn't you leave a comment on denbuzze's answer? Jun 4, 2015 at 5:13
  • 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
    Sep 2, 2019 at 14:06
9

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.

3
3

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.

2

commit and push to master..

then :

git checkout gh-pages  // -> go to 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
2

OR you can just use the cmd below, this will push your local master branch to gh-pages master branch. git push -f origin master:gh-pages

0
1

UPDATE: GitHub now allows pages to be published from any branch and directory you want.


It was much easier for me to use the gh-pages branch as master. There's nothing magical about "master"; it's just another branch name. There is something magical about gh-pages, because that's where GitHub is looking for index.html to serve your page.

Read more in my other answer on this topic.

Using gh-pages as master is also easier than subtrees, which are easier than mirroring. You could use git subtree as described here or here: if you have a directory which contains your demo, you can push that directory to the gh-branch with one command. Let's say you name the directory gh-pages to make things clear. Then after you've committed and pushed your changes to master, run this to update gh-pages:

git subtree push --prefix gh-pages origin gh-pages

The problem is if your files in gh-pages refer to files in other directories outside it. Symlinks don't work, so you'll have to copy files in the directory that serves as gh-pages.

If you use gh-pages as master, this problem won't occur.

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.