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.
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.
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:
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:
And the command prompt should alert you that you've switched to a new branch.
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!
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.
And now you should be able to see the the branch on your Github page.
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:
If you ever forget what branch you are on, you can always check with this command:
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:
At times you may encounter errors when trying to push from the master branch to the gh-pages branch.
An easy way to fix this is to just delete the gh-pages branch:
And you can delete local branches like this:
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:
The posts on this site are written and maintained by Jim Lynch. About Jim...