Create a blog with Hugo and Github

2023/08/22

  1. home brew install hugo

  2. create two repos

create repo blog and repo wenting-wang.github.io

notice: make sure it is [name].github.io

  1. clone both repos to home dir

make an init commit

cd wenting-wang.github.io
git checkout -b main
touch README.md
git add .
git commit -m "init"
git push origin main
  1. create new site
cd blog
hugo new site wenting-wang-blog
  1. add submodule
cd blog/wenting-wang-blog
git submodule add -b main https://github.com/wenting-wang/wenting-wang.github.io.git public

debug if it does not work

  1. get a theme
cd blog/wenting-wang-blog/themes
git clone https://github.com/yihui/hugo-xmin.git

# start with the default example
cp -a ~/blog/wenting-wang-blog/themes/hugo-xmin/exampleSite/. ~/blog/wenting-wang-blog/
  1. edit config file, move/create post to content/post. have a look
hugo server
  1. deploy site
cd /blog/wenting-wang-blog
hugo -t hugo-xmin
cd public
git add .
git commit -m "update"
git push origin main
  1. back-up files
cd blog
git add .
git commit -m "update"
git push origin main
  1. you could also use the script for step 8(deploy.sh) and 9(sync.sh) by running
# cd blog/wenting-wang-blog
./deploy.sh

and

# cd blog
./sync.sh

deploy.sh:

#!/bin/sh
# path: ~/blog/wenting-wang-blog/deploy.sh

# If a command fails then the deploy stops
set -e

# Print out commands before executing them
set -x

printf "\033[0;32mDeploying updates to GitHub...\033[0m\n"

# Build the project.
hugo -t hugo-xmin

# Go To Public folder
cd public

# Add changes to git.
git add .

# Commit changes.
msg="rebuilding site $(date)"
if [ -n "$*" ]; then
	msg="$*"
fi
git commit -m "$msg"

# Push source and build repos.
git push origin main

# Back to the origin folder
cd ..

# remove the local public folder
# git rm --cached public -rf
# rm -rf public

sync.sh:

#!/bin/sh
# path: ~/blog/sync.sh

# Use:
# ./sync.sh "commit messages"

# If a command fails then the deploy stops
set -e

# Print out commands before executing them
set -x

# printf "\033[0;32mSync blog data to GitHub...\033[0m\n"

# Add changes to git.
git add .

# Commit changes.
msg="rebuilding site $(date)"
if [ -n "$*" ]; then
	msg="$*"
fi
git commit -m "$msg"

# Push source and build repos.
git push
  1. domain

the default domain is [name].github.io, same as your repo name. e.g. wenting-wang.github.io

if you have another domain, confirm on the website from where you bought it(e.g. google domain), so as on Github Page