Today, I am going to be showing you how to host your own static website using GitHub Pages. For this guide, I am going to assume that you can either learn HTML as you go or already have a tiny bit of knowledge on it.
that’s it.
*arguably, you can use an iPad with a proper Git client and a great text editor from the App Store, but generally that’s not free.
A repository holds all of the code for your project – you can think of it as a place to hold your website or any other project you want to make. So first, let’s name it. Choose any name you want for your website, but keep in mind that it must be URL accessible.
Some example names and their URLs
name | site url, where treasure-hacks would be replaced with your username |
---|---|
treasurehacks-site | https://treasure-hacks.github.io/treasurehacks-site |
site | https://treasure-hacks.github.io/site |
super-octo-doodle | https://treasure-hacks.github.io/super-octo-doodle |
treasure-hacks.github.io | https://treasure-hacks.github.io/ |
If you put username.github.io
, where username
is your GitHub username, your end URL will be the
root subdomain (username.github.io). Otherwise, it will be username.github.io/repo-name/
You can do this one of two ways:
For ease of use, we will be using the first method in this tutorial. If you are comfortable with the command line, you can look at the git documentation to find the equivalent actions.
main
, and other
branches you make are based on the latest version of main at time of creating.
main
to avoid any
confusion you might have with the concept of branches.
https://github.com/username/repo-name
, where username is your GH username and Once you get here, congratulations! Your repository is now on your computer, ready for you to make changes.
Now, let’s make the first change to your project. Let’s open up an IDE (integrated development environment) such as VS Code and get started.
You don’t have to use VSCode – in fact you can use any text editor (TextEdit, Notepad, Notepad++, TextMate, etc.) – but an IDE like VS Code or Webstorm will help you track changes, give you syntax highlighting, and so much more. For this tutorial, I’ll use VSCode.
You should see something like this when you first open VSCode:
Open...
(under “Start”) and find the local
path of your repo. Choose the folder that matches the name of your repo, e.g. super-octo-doodle
or site
.
index.html
!
then hit enter. Change the title of the doc,
and add some text or HTML of your choice into the editor.
Save the file, and you can now open it in your browser. You should see this:
https://username.github.io/repo-name
(if the name of the repository is
username.github.io, omit the Fantastic – you are now able to make changes locally, do a basic load test in the browser, and push to GitHub to create your website!
But, there’s one problem right now: local files. They have some of the behavior of a URL, but they’re not quite the same. Requests often won’t work, pointing to a folder doesn’t load the index.html file, and other browser features are disabled. To fix this, keep reading on how to start a local http server to test out the changes.
Here’s some helpful information on why you should use a local server instead of just using file and on how to get it set up. Follow this until you’ve installed Python. If you’re on a Mac, python is preinstalled with the machine. https://developer.mozilla.org/en-US/docs/Learn/Common_questions/set_up_a_local_testing_server
ctrl + `
(control and the key above the tab key) to open up your command
line. (on a Mac it is also control, not cmd)
python -V
# If Python version returned above is 3.X
python3 -m http.server
# On windows try "python" instead of "python3", or "py -3"
# If Python version returned above is 2.X
python -m SimpleHTTPServer
localhost:8000/
, where the path is highlighted in orange). Remotely, it is
served at
username.github.io/repo-name/
To fix this, we can stop our server with control + c (not cmd + c), and change
directory to parent by typing this:
cd ..
Your command window should show username@device-name GitHub %
or something like that,
where GitHub is the parent folder of your repository.
localhost:8000/repo-name
, and you should see your website
again, but this time at the correct path:
Nice how the only difference between your local URL (localhost:8000Awesome! You’ve set up your own local development server to test your webpages on.
Using the knowledge you’ve just gained, here’s a process on how you can make changes to your website:
cd ..
as mentioned
previously.
localhost:8000/repo-name/
That’s the basics of testing and hosting a website using GitHub pages. It may seem like a lot of work, but you’re learning concepts that you can apply to future coding ventures that will often have you set up GitHub repositories and run local servers to test your applications. I hope you found this helpful, and I can’t see what you make!
Please feel free to ask any questions, share your GitHub websites, or talk about the process and/or get advice/tips related to coding your site.