How to Deploy Your Website Using GitHub Pages (Free Hosting)

how to deploy website using GitHub Pages

If you’re a developer, designer, student, or hobbyist who has built a static website, you’ll want to share it with the world. Hosting a website can be expensive or technically complex, but fortunately, GitHub Pages offers a free, reliable, and simple solution. GitHub Pages is a feature offered by GitHub that allows users to host static websites directly from their GitHub repositories. 

It eliminates the need to buy a hosting plan or configure a server. Instead, you can deploy your website in just a few steps, with GitHub taking care of the backend. Whether you’re showcasing a portfolio, publishing documentation, or sharing a personal blog, GitHub Pages makes it incredibly easy to go live with your project. This article will walk you through the entire deployment process, starting from setting up your repository to launching your website and even customizing it further.

Prerequisites

Before diving into deployment, it’s important to make sure you’re prepared with a few essentials. First, you need a GitHub account. This will be your primary tool for hosting and managing your website files. Creating an account is free and straightforward—just visit GitHub.com and sign up. Second, you need your static website ready. This means having files like index.html, style.css, and any JavaScript or image files you plan to use. While it’s helpful to know the basics of Git and the command line, it’s not strictly necessary, as GitHub’s interface allows for direct uploads and edits. Having Git installed on your local machine is optional but beneficial if you want to manage your files through the terminal.

Step 1: Create a GitHub Repository

The first step in publishing your website is to create a repository on GitHub, which will store all of your website files. To do this, log in to your GitHub account and click the “+” icon at the top right of the page. Choose “New repository” from the dropdown menu. You’ll be asked to enter a repository name. If you’re deploying a personal site, this name must follow a specific format: it should be yourusername.github.io. This format is used for user or organization sites and results in your site being hosted at https://yourusername.github.io. If you’re deploying a project-specific site, you can name the repository anything you like, such as portfolio or myproject. This kind of site will be hosted at a sub-path like https://yourusername.github.io/project-name.

After naming your repository, choose the public visibility option to ensure it can be accessed by everyone. You may optionally initialize the repository with a README file, but this is not necessary for deployment. Once all the settings are in place, click “Create repository.” You now have a space where your website files can live.

Step 2: Add Website Files to the Repository

Once your repository is created, the next step is to upload your website files. There are two main methods for doing this: through GitHub’s web interface or via Git commands from your local machine.

To upload files through the GitHub interface, navigate to your repository’s main page. Click the “Add file” button, then choose “Upload files.” You can now drag and drop your HTML, CSS, and JavaScript files into the upload area. After uploading all your files, click the “Commit changes” button to save them to your repository.

If you prefer to use Git on your local machine, open your terminal or command prompt and navigate to your project directory. Begin by initializing Git in your folder using git init. Then connect your local folder to your remote GitHub repository by running git remote add origin https://github.com/yourusername/your-repo-name.git. Next, stage all files for commit with git add ., and commit them using git commit -m “Initial commit”. Finally, push your files to GitHub using git push -u origin main. After these steps, your website files will be live on GitHub.

Step 3: Configure GitHub Pages

Now that your files are on GitHub, it’s time to publish them using GitHub Pages. Go to your repository and click on the “Settings” tab. On the left-hand sidebar, scroll down and find the “Pages” section. This is where you configure the source for GitHub Pages. You’ll see an option to choose the source branch. Select “main” from the dropdown if your files are on the main branch. If you placed your site files in a subfolder like /docs, choose that folder as the source.

After selecting the source, click “Save.” GitHub will automatically process your site and display a link to your live website at the top of the Pages section. This link will be in the format https://yourusername.github.io or https://yourusername.github.io/repo-name, depending on whether you created a user or project site. It may take a few minutes for the site to become live, especially if this is your first deployment.

Step 4: Verify Your Site

Once GitHub has processed your site, it’s time to check if everything is working correctly. Click the provided URL and see if your homepage loads. Ideally, you should see your website exactly as you built it. However, if something isn’t working, there are a few common problems you can troubleshoot.

If the page displays a 404 error, double-check that your repository contains a file named index.html in the root directory or selected folder. GitHub Pages looks for this file as the entry point to your site. If your CSS or JavaScript files aren’t loading, ensure that the file paths in your HTML are correct. Paths should be relative and correctly spelled. Also, verify that your GitHub Pages settings are pointing to the correct branch and folder. Sometimes clearing your browser cache or waiting a bit longer can resolve temporary issues.

Step 5: Customize Your Site (Optional)

GitHub Pages doesn’t just stop at hosting. It also supports Jekyll, a static site generator that lets you create beautiful websites with themes and markdown support. To activate Jekyll features, you can create a _config.yml file in the root of your repository. For example, adding theme: minima will apply a simple, elegant theme to your site. This is especially useful for blogs or documentation.

Another great feature of GitHub Pages is the ability to use a custom domain. If you’ve purchased a domain from a registrar like Namecheap or GoDaddy, you can link it to your GitHub Pages site. Start by creating a file named CNAME in your repository’s root directory. In this file, write your domain name, such as www.example.com. Then go to your domain registrar and update your DNS settings. You’ll need to add a CNAME record that points your domain to yourusername.github.io. For root domains (without www), you may need to set A records pointing to GitHub’s IP addresses. Once the DNS changes propagate, go back to your GitHub repository settings and enable HTTPS for secure browsing.

Step 6: Advanced Deployment Options

For more advanced users, GitHub Pages offers powerful deployment workflows. One such method is using a separate branch called gh-pages. This is useful when you want to keep your source code separate from the compiled website files. You can generate the build files using a static site generator or build tool, then push those files to the gh-pages branch. After setting the source in GitHub Pages settings to gh-pages, your site will be live with a clean separation between code and content.

Another powerful feature is GitHub Actions, which allows for automation. With GitHub Actions, you can set up a workflow file that automatically builds and deploys your site every time you push changes. This is especially helpful for projects using frameworks like React, Vue, or static site generators like Hugo. A sample workflow includes steps like checking out the repository, installing dependencies, running the build script, and publishing the output folder to GitHub Pages using an action like peaceiris/actions-gh-pages. This automation saves time and ensures that your website is always up to date with the latest changes from your main branch.

If your project contains folders that begin with an underscore (e.g., _includes, _layouts), GitHub might ignore these due to Jekyll processing rules. To avoid this issue, simply create a file named .nojekyll in the root directory of your repository. This file tells GitHub Pages to bypass Jekyll processing entirely, ensuring all your files are served correctly.

Step 7: Troubleshooting Tips

Even though deploying a site with GitHub Pages is generally straightforward, you may encounter issues. The most common problem is a 404 error, which usually means the index.html file is missing or in the wrong location. Always double-check that this file exists in the root of the branch or folder you’re deploying from.

If your styles or scripts aren’t loading properly, it’s often due to incorrect paths. Relative paths are recommended, such as ./style.css or ./js/app.js. Make sure capitalization is consistent as GitHub Pages is case-sensitive. Another issue could be that your changes aren’t showing up after a new commit. In this case, try clearing your browser cache or doing a hard refresh. For custom domain issues, verify that your DNS records are set up correctly and that your CNAME file matches your domain exactly. You can use DNS propagation checkers like whatsmydns.net to monitor changes.

If your site is not updating even after changes have been pushed, go back to your Pages settings and confirm the source is still set correctly. Sometimes it helps to toggle the source off and back on again to re-trigger deployment.

Conclusion

Deploying a website with GitHub Pages is an accessible and powerful way to publish your work online. From creating a repository to uploading your files and configuring settings, the process is designed to be user-friendly and efficient. GitHub Pages is especially appealing because it’s free, supports HTTPS out of the box, and integrates seamlessly with Git version control. It’s an ideal solution for developers looking to share projects, students building portfolios, and anyone wanting to create a web presence with minimal cost and effort.

Once your site is live, you can take it even further. Explore the use of Jekyll to transform your static site into a dynamic and themeable website. Add a custom domain to give your site a more professional touch. Implement GitHub Actions to streamline your build and deployment process. And most importantly, keep updating your content and experimenting with new ideas. GitHub Pages gives you the platform, you bring the creativity. With these tools in your hands, the web is yours to explore and conquer.

Scroll to Top