Building up a website by using Hugo and GitHub

Table of Content

You can do the following instructions to build up a website by using Hugo and GitHub with Markdown files.

Preparing Hugo to generate a local website

You need to download and install Hugo, create a new website, and then add a theme. Afterwards, you can generate a local website with Markdown files.

Downloading Hugo

  1. Navigate to https://gohugo.io/ and click Quick Start.

  2. Click the install hyperlink to navigate to the Install Hugo page.

  3. Click the Hugo Releases hyperlink to navigate to the GitHub page.

  4. Scroll down to the Assets area to find the Hugo installation package for your computer.

    For example, the Hugo installation package for 64-bit Windows operating system:

    hugo for Windows 64-bit

  5. Save the downloaded ZIP file and un-zip the file in your local computer.

Installing Hugo

  1. Open the Hugo installation package folder and you can see the following three files.

    3 hugo files

  2. Copy the hugo.exe file to a newly created folder.

    Note: Do not use space or non-English language words in the folder name or the upper level folders. For example, you can create a new folder as D:\Hugo\bin.

  3. Open the computer cmd command panel and navigate to the new folder you just created.

  4. Type hugo in the cmd command line and press Enter.

    Expected outcome:

    You may see the following error message and you can ignore it.

    Error: Unable to locate config file or config directory. Perhaps you need to create a new site. Run "hugo help new" for details.

  5. Set the environment variable by adding the path of the new folder where the hugo.exe file exists.

  6. To verify hugo installation, open a new cmd command panel and type hugo version.

    Expected outcome:

    Hugo version is displayed. It means that Hugo is installed successfully.

Creating a new site

  1. Open the computer cmd command panel and navigate to the new folder containing the hugo.exe file. For example, D:\Hugo\bin.

  2. To create a new site, enter the command:

    hugo new site your-new-site-name
    

    For example:

    hugo new site DocWeb
    

    Expected outcome:

    A new folder named as your-new-site-name (for example, DocWeb) is created, with multiple folders and files inside.

Adding a theme

  1. Navigate to https://themes.gohugo.io/ to find a theme you like.

  2. Click the theme to download a ZIP file to your local computer, and un-zip the theme ZIP file.

  3. Copy the theme file to the themes folder which is created in Creating a new site.

  4. To use the theme format, copy three items (content, static and config.toml) in the exampleSite folder of the themes folder, and overwrite the three same items in the upper level folder which is rooted under your new site folder.

    copying theme files

Preparing Markdown files

  1. Navigate to the content folder which is rooted under your new site folder.

  2. Edit the Markdown files.

    Note: You can edit Markdown files by checking the instructions in Markdown instructions.

Generating a local website

  1. Open the computer cmd command panel and navigate to the new folder containing the hugo.exe file. For example, D:\Hugo\bin.

  2. Type the command line:

    hugo server
    

    Expected outcome:

    You can view your website through localhost:1313.

Preparing GitHub

You need to create a new repository in GitHub, install Git and configure SSH key in GitHub.

Creating a new repository in GitHub

  1. Navigate to https://github.com/ to register a new GitHub account.

  2. Create a new repository.

    Note: Your repository name must follow the format as “your-account-name.github.io”. For example, Bunny-Xiangli.github.io.

Installing Git

  1. Navigate to https://git-scm.com/downloads/ to download the Git installation package for your computer.

    Expected outcome:

    A ZIP file (for example, Git-2.30.0.2-64-bit.exe) is automatically downloaded.

  2. Un-zip the file and install Git with default configurations.

    Expected outcome:

    You can see that Git Bash and Git GUI applications are installed successfully.

Configuring SSH key in GitHub

  1. Open the Git Bash application.

  2. Enter the command:

    cd ~/.ssh
    
  3. Enter the command:

    ssh-keygen -t rsa -C "your-email-address"
    

    Expected outcome:

    Two files (id_rsa and id_rsa.pub) are generated in the .ssh folder under your user account.

  4. Log in to https://github.com/ as your GitHub account.

  5. On the top-right corner, select Settings in your account drop-down menu.

  6. Click SSH and GPG keys on the left side panel.

  7. Click New SSH key and add the content of id_rsa.pub file. For example, you can see the added SSH in the following image.

    add ssh key

Building up an online website

  1. Navigate to the folder which contains your Markdown files (for example, D:\Hugo\bin\DocWeb\content\posts).

  2. Right-click and select Git Bash Here in the drop-down menu.

    git bash here

  3. To link your theme with the GitHub repository, enter the command:

    hugo  --theme=your-github-theme --baseUrl="https://your-github-name.github.io"
    

    For example:

    hugo --theme=hugo-theme-bootstrap4-blog --baseUrl="https://bunny-xiangli.github.io/"
    

    Expected outcome:

    A public folder is created under your Hugo site folder, with multiple sub-folders and files used for generating HTML.

    public folder

  4. To navigate to the public folder, enter the command:

    cd public
    
  5. To initiate GitHub, enter the command:

    git init
    
  6. To add your local files to GitHub repository, enter the command:

    git add .
    
  7. To commit files in Git repository, enter the command:

    git commit -m 'first commit'
    

    In the command, you can replace first commit with your own descriptions.

  8. To create the connection between your local and GitHub repositories , enter the command:

    git remote add origin your-git-repsitory-name
    

    For example:

    git remote add origin https://github.com/Bunny-Xiangli/Bunny-Xiangli.github.io.git
    
  9. To push your website to GitHub, enter the command:

    git push -u  origin master 
    
  10. In your GitHub repository, go to Settings , and scroll down to find the GitHub Pages area.

  11. Select Branch:master in the drop-down menu and click Save.

    github pages setting

  12. Navigate to the website of your GitHub Pages (for example, https://bunny-xiangli.github.io/).

    Expected outcome:

    You can view the online website successfully.

Read more →