How to Deploy Your Hugo Site to GitHub Pages - A Simple Step-by-Step Guide
How to Deploy Your Hugo Site to GitHub Pages: A Simple Step-by-Step Guide
Many guides online, including the Hugo documentation, tend to complicate the process of deploying a Hugo site to GitHub Pages. They often suggest creating multiple repositories or using submodules, which can be unnecessary. This guide aims to simplify the process, making it as straightforward as possible.
Prerequisites
Before you begin, ensure the following:
- You have a GitHub account.
- You have a text editor installed.
- You have a fully functioning local Hugo site. This means you can run your site locally by using
hugo serve -D
in your command prompt while in the Hugo site folder. This guide specifically covers hosting a local, working site on GitHub Pages.
Step 1: Creating the GitHub Repository
First, your local Hugo folder needs a place on GitHub.
- Go to your GitHub profile page and navigate to Repositories > New.
- Under Repository name, enter the exact name of your Hugo site folder and click Create Repository.
- You’ll be directed to a page with an HTML link; copy this link.
Step 2: Preparing Your Hugo Folder
Next, we need to adjust your local Hugo folder for web deployment.
- Open your Hugo site folder in your preferred text editor.
- In the
config.toml
file, add the following line at the top:publishDir = "docs"
- Find the line that says
baseURL = "https://example.org"
and replace the URL with:Make sure the URL follows this format exactly.baseURL = "https://yourgithubname.github.io/yourgithubproject/"
- Save the file.
Step 3: Connecting Your Hugo Folder to the GitHub Repository
Now it’s time to push your files to the GitHub repository.
- Open Command Prompt and navigate to your Hugo site folder.
- Run
hugo serve
to generate thedocs
folder we configured earlier. - Initialize an empty Git repository by entering
git init
. - Add your GitHub repository as the remote origin with:
git remote add origin <paste the link you copied from the "Creating the GitHub Repo" step>
- Check the status of your files with
git status
—you should see uncommitted items in red. - Stage all your files with
git add --all
. - Commit the files with:
git commit -m "initial commit"
- Push your files to GitHub with:
git push -u origin master
Step 4: Enabling GitHub Pages
Finally, let’s set up GitHub Pages to host your website.
- In your GitHub repository, go to Settings.
- Scroll down to GitHub Pages and under Source, select master branch /docs folder and click Save.
- Refresh the page and scroll back down to GitHub Pages—you should see a link to your new website!
And that’s it! Your Hugo site is now live on GitHub Pages.