2

I am using SourceTree in combination with a Github repository for version control for a project. For a while, I've been using master to commit all new changes, but every now and then, I want the gh-pages branch to fast-forward to the latest commit of master, so that I can update the live page with the page I have in production (usually when a major update is finished).

I have tried rebasing, as another SO post suggested, but this made the problem even worse, as SourceTree had me manually choose what changes I wanted and the rebasing took a little while especially considering the amount of commits between the last update of gh-pages. After successfully rebasing in one project, I could basically sync the gh-pages branch to master whenever I desired and all in-between versions would commit automatically (I could not reproduce the same behavior in another repository though). However, what I want is to get the last commit and overwrite all files. To do this, I usually just copy the whole folder while on master branch to another location, then switch to gh-pages and overwrite all files manually. This is sub-optimal, though and can get really problematic for larger projects.

So, what I want and need is to automate this procedure, either through SourceTree or via a script.

TL;DR: I need a way to update gh-pages to the latest master commit semi-automatically, which will overwrite all files with the ones in master without rebasing and will then push them to the Github repository.

Angelos Chalaris
  • 6,611
  • 8
  • 49
  • 75

2 Answers2

3

However, what I want is to get the last commit and overwrite all files.

You should:

  • merge gh-pages to master with the --ours option (so master is actually untouched).
  • merge master to gh-pages (which means gh-pages fast-forwards to master and becomes identical)

So:

git checkout master
git merge --ours gh-pages
git checkout gh-pages
git merge master

Don't forget though that since a few days ago, you don't have to maintain a gh-pages branch anymore.

Simpler GitHub Pages publishing now allows to keep your page files in a subfolder of the same branch (no more gh-pages needed):

Now you can select a source in your repository settings and GitHub Pages will look for your content there.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Should I execute that sequence of commands while on `master` or while on `gh-pages`? The idea of not maintaining a `gh-pages` branch sounds kind of nice for a change actually, as I can probably work on the rest of the page and the live version at the same time, without breaking anything. Thanks! – Angelos Chalaris Aug 19 '16 at 07:11
  • 1
    @AngelosChalaris That sequence of commands incudes setting the current branch. So you can start it from any branch (preferably in a clean workspace) – VonC Aug 19 '16 at 07:13
  • Right, thanks a whole lot for your answer and for showcasing this new feature! – Angelos Chalaris Aug 19 '16 at 07:16
0

For those who have created their app with create-react-app.

this was the only method that worked for me without errors

  1. Add this snippet in your /public/index.html to solve the browsers caching issue

<meta http-equiv='cache-control' content='no-cache'> 
<meta http-equiv='expires' content='0'> 
<meta http-equiv='pragma' content='no-cache'>
  1. change the default breach from gh-pages to master (guide)

  2. delete gh-pages Branch Both Locally and Remotely (guide)

  3. Run: npm run deploy. This will build your app and re-create gh-pages branch with updated content.

  4. choose the gh-pages branch as publishing source and the /(root) as a folder for your publishing source.(guide)

  5. It may take some time. After a minute visit your site and it should be updated by now.

(ref).

if you have an alternative please share!

AbdelghanyMh
  • 314
  • 2
  • 8