Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

Using Github To Host Your Website for Free

4/17/2016

0 Comments

 
I'm a front-end developer, and I have a lot of source code in various repositories on github. The code is public and open source so it's free for me to just leave the code there and create as many repositories as I need. Actually hosting a website is much different from storing source code, but wouldn't it be nice if you could just have github also just host the dist files for your site as well? Well, it turns out you can! The best part is that this basically allows you to host unlimited open source websites and web apps for free with the simple and familiar (to me, at least) interface of git and github. Below is an example of how I've been doing this.

tl;dr

git subtree push --prefix dist/ origin gh-pages
To host your site on Github for free, just run this command:

Ahh, The Command Line :)

If you've been using a git client with a gui up until now then you might be a little uncomfortable with the command line. Unfortunately, most gui interfaces don't have the capabilities for some of these commands so you'll need to crack open that command line and navigate into the root of your project.

I boot up terminal, clone the repo (if I don't already have it on my computer), and cd down into the root directory of my repository. 

Build Your dist Folder

In your project you will many times have a build script that does things like minify your code, convert scss, etc. It then drops the output files into a folder that can really be called anything you want, but I normally call it dist (default for the Gulp-Angular generator). You normally take these files and put then on a webserver, but as we'll see there's a special github branch this will provide you with free hosting. I usually run gulp to build the project files. Go ahead and do this. Now you're ready to move the contents of dist​ to the gh-pages branch. 

The gh-pages Branch

Normally, you make branches planning to merge them back into the master. But, in thise case you want to make an "orphan brach" that is not meant to be merged back. In fact, setting it up like it's a normal branch will give you a lot of headaches.

Remove Your dist Folder From the .gitignore ​File

MY yeoman generator automatically adds the dist folder to .ignore, and if you have it in there then you won't be able to push your gh-pages branch, so yeah, edit the .gitignore file, remove the /data line, and save the file.
vim .gitignore

Clone the Master Repo

You're going to want to have the main reposiroty cloned on your local machine. If don't already have this, create and empty folder for it, cd into the new folder, and run the clone command with the HTTPS url of your repository:
git clone YOUR_REPO_URL

Create a gh-pages Branch

Github will host everything in this branch for you for free, but only for the branch named "gh-pages". Therefore, the next thing to do is create a branch called gh-pages. Note that most branches are meant to eventually be merged back into master. The gh-pages branch is different. It is not meants to ever be merged, and it is sort of just there independent of the master branch. This is why we check it out with the orphan tag like so: 
git checkout --orphan gh-pages
And the command prompt should alert you that you've switched to a new branch.
Picture

Clean Out the gh-pages Branch

You'll notice that everything in  the master branch was copied over into the gh-pages branch. This is, well ,not what we want so we can just delete everything in the folder. Man, I love running the rm -rf command. haha. Don't worry, you aren't won't lose all your files. They're still saved in the master branch!​  
rm -rf *

Commit and Push the gh-pages Branch

If you go back to your Github page and check for the gh-pages branch you'll notice that it's not there. Well, that's because you have to push the branch up to Github, duh! So let's do that now.
git commit -a --all -m "Cleanin' out the gh-pages"
git push origin gh-pages
And now you should be able to see the the branch on your Github page.
Picture

Switch Back to Master Branch

Alright, your gh-pages branch is all set up and ready to go! Now, let's flip back to the master branch:
git checkout master
If you ever forget what branch you are on, you can always check with this command:
git branch

Push Your Dist Files to the gh-pages Branch

When publishing your site live, you don't need to throw all the source code up there. If you have some type of build process or minification step then you'll probably have some type of dist folder with just the files that are meant to be hosted for the end user to see in their browser. With this command, you can push only a subdirectory of your master repo like this:
git subtree push --prefix dist/ origin gh-pages

Fast-Forward Errors

At times you may encounter errors when trying to push from the master branch to the gh-pages branch. 
Picture
An easy way to fix this is to just delete the gh-pages branch:
git push origin :gh-pages
And you can delete local branches like this:
git branch -D gh-pages
Then you should be able to push to gh-pages as usual.

Test Your Site

You're all done! Now your should be abel to test your site at this url:
http://jimtheman.github.io/Sure-Fire-Solar-Site/

Related Links

  • http://blog.teamtreehouse.com/using-github-pages-to-host-your-website
  • https://education.github.com/git-cheat-sheet-education.pdf
  • https://gist.github.com/cobyism/4730490

0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Picture
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blockchain
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Investing
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    March 2023
    August 2021
    February 2021
    January 2021
    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2023