So, you've built a killer website using Hugo, and now you're ready to unleash it upon the world? Awesome! GitHub Pages is a fantastic (and free!) way to host your static sites. This guide will walk you through the entire process, step-by-step, making it super easy to get your Hugo site live. Let's dive in, guys!

    Prerequisites

    Before we get started, make sure you have the following:

    • A Hugo Site: Obviously, you'll need a Hugo site ready to go. If you don't have one yet, go create one.
    • A GitHub Account: You'll need a GitHub account to host your site. If you don't have one, sign up for a free account at github.com.
    • Git Installed: Git is a version control system that you'll use to manage your code and push it to GitHub. If you don't have Git installed, download and install it from git-scm.com.

    Step 1: Create a GitHub Repository

    The first thing you need to do is create a new repository on GitHub. This repository will hold all of your Hugo site's source code and the generated static files.

    1. Go to github.com and log in to your account.
    2. Click the "+" button in the top right corner and select "New repository."
    3. Give your repository a name. There are two main approaches here:
      • User/Organization Repository: If you want your site to be accessible at yourusername.github.io (or yourorganization.github.io), you must name the repository yourusername.github.io (or yourorganization.github.io). This is a special naming convention that GitHub Pages recognizes. This method allows you to host a website at the root of your GitHub Pages domain.
      • Project Repository: If you want your site to be accessible at yourusername.github.io/your-repository-name, you can name the repository anything you like (e.g., my-hugo-site). This method allows you to host multiple websites on your GitHub Pages domain, each in its own subdirectory.
    4. Add a description (optional).
    5. Choose whether to make the repository public or private. For GitHub Pages, the repository must be public.
    6. Important: Do not initialize the repository with a README, .gitignore, or license. We'll add these later.
    7. Click "Create repository."

    Step 2: Generate Your Hugo Site

    Now that you have a GitHub repository, it's time to generate your Hugo site. This process creates the static HTML, CSS, and JavaScript files that will be served by GitHub Pages.

    1. Open your terminal or command prompt.

    2. Navigate to the root directory of your Hugo site.

    3. Run the following command to generate the site:

      hugo
      

      This command will generate the static site files in the public directory by default. You can configure the output directory in your config.toml file if you prefer a different location.

    Step 3: Initialize Git and Add Your Site to the Repository

    Next, you need to initialize a Git repository in your Hugo site's directory and add the generated files to it. This will allow you to track changes to your site and push them to GitHub.

    1. Navigate to the public directory (or your configured output directory) in your terminal.

    2. Run the following commands:

      git init
      git add .
      git commit -m "Initial commit of generated site"
      
      • git init initializes a new Git repository in the current directory.
      • git add . adds all the files in the current directory to the staging area.
      • git commit -m "Initial commit of generated site" commits the staged files with a descriptive message.

    Step 4: Connect Your Local Repository to GitHub

    Now, you need to connect your local Git repository to the remote repository you created on GitHub. This will allow you to push your local changes to GitHub.

    1. Go to your GitHub repository page and copy the repository URL. It will look something like this:

      https://github.com/yourusername/your-repository-name.git
      
    2. In your terminal, run the following command, replacing your-repository-name with the actual name of your repository:

      git remote add origin https://github.com/yourusername/your-repository-name.git
      

      This command adds a remote connection named origin to your GitHub repository.

    Step 5: Push Your Site to GitHub

    Finally, you're ready to push your generated site files to GitHub. This will make your site live on GitHub Pages.

    1. Run the following command in your terminal:

      git push -u origin main
      
      • git push pushes your local commits to the remote repository.
      • -u origin main sets the upstream branch to origin/main, so you can simply use git push in the future.

      Note: If you are using an older version of Git, the default branch might be master instead of main. In that case, use git push -u origin master instead.

    Step 6: Enable GitHub Pages

    After pushing your site to GitHub, you need to enable GitHub Pages for your repository. This will tell GitHub to serve your static files.

    1. Go to your GitHub repository page.
    2. Click on the "Settings" tab.
    3. Scroll down to the "GitHub Pages" section.
    4. In the "Source" section, select the branch you pushed your site to (usually main or master) and the / (root) directory. Important: Make sure you select the correct branch where your public directory contents are located.
    5. Click "Save."

    GitHub Pages will now build and deploy your site. This process may take a few minutes. You can check the status of the build in the "Actions" tab of your repository.

    Step 7: Access Your Live Site

    Once GitHub Pages has finished building and deploying your site, you can access it at the following URL:

    • User/Organization Repository: yourusername.github.io (or yourorganization.github.io)
    • Project Repository: yourusername.github.io/your-repository-name

    Congratulations! Your Hugo site is now live on GitHub Pages!

    Managing Your Hugo Source Code Separately (Optional)

    You might not want to keep your Hugo source files (the actual Hugo project with the layouts, content, etc.) inside the public directory. That's perfectly fine! Here's how you can manage them separately:

    1. Keep Your Hugo Project Separate: Maintain your Hugo project in a directory outside the public directory. This is your main Hugo project where you edit your content, layouts, and configuration.
    2. .gitignore File: Create a .gitignore file in the root of your Hugo project (the one containing config.toml). Add the public directory to this file. This tells Git to ignore the contents of the public directory in your main Hugo project.
    3. Dedicated Repository for Source (Optional): Create a separate GitHub repository for your Hugo source code (the project excluding the public directory). This is optional, but a good practice for backing up and version controlling your source files.
    4. Workflow:
      • When you make changes to your Hugo site, run hugo to generate the site into the public directory.
      • Navigate to the public directory.
      • git add ., `git commit -m