Build Hexo Blog Deploying On Github

This is step-by-step guide walks you through creating a fast, static blog use Hexo, deploying it seamlessly to GitHub Pages for free hosting,
and styling it with Melody theme. One-time setup(config github, dependencies), maintain & develop by using Markdown,
modify js/css without worry about database.

Prerequisites

  • Install Node.js and npm on your system.
  • Set up a GitHub account and create a new repository (e.g., username.github.io for GitHub Pages).Don’t need to git clone this new repo.

Solution1: Use Main Branch

Install Hexo

1
npm install -g hexo-cli
1
hexo -v

Initalize Your Blog and install Melody(for Hexo v5+)

Create a new Hexo project:

1
$ hexo init blog

Navigate to the project directory:

1
2
3
$ npm install
$ npm install hexo-theme-melody
$ npm install hexo-renderer-pug hexo-renderer-stylus --save

Configure Hero and theme

Edit the _config.yml file in the root directory of your project:

  • Update site details:
1
2
3
4
5
6
7
8
url: https://username.github.io

theme: melody

deploy:
type: git
repo: git@github.com:username/username.github.io.git
branch: main
  • For Hexo v5+, copy the default Melody configuration:
    Create _config.melody.yml in your root directory.
    Copy contents from node_modules/hexo-theme-melody/_config.yml into _config.melody.yml.

Install Hexo Git deployer

1
$ npm install hexo-deployer-git --save

Verify in Local

1
$ hexo clean && hexo g && hexo s

Push to production

1
$ hexo clean && hexo g && hexo d

Deploy your blog

  • Go to your blog repo in github UI -> Settings -> Pages -> Source build choose Deploy from a branch -> Branch choose main /root save
  • Go to your terminal, under blog repo:
1
$ hexo clean && hexo g && hexo d

Quick Start

Create new blog

1
$ hexo new Post

Modify style

  1. Add /css/custom.css under source/_posts
  2. In _config.melody.yml file:
1
2
stylesheets:
- /css/custom.css

Insert picture to certain blog

  1. Set post_assert_folder: true in _config.yml file.
  2. When type hexo new postname, under source/_posts, generate one file called postname.md and folder postname.
  3. Put images under postname folder.
  4. Put code below inside of blog:
1
{% asset_img <Image-Name>.png <Alta-text> %}

Create new Page

1
2
3
$ hexo new page tags
$ hexo new page categoriess
$ hexo new page about

Solution2: Seperate Branch

In order to upload all source code to github, so need to seperate source code & deployment branch. Hexo source code and deployment is seperated.

Step1 Create new repo

create a new repository (e.g., username.github.io for GitHub Pages).Don’t need to git clone this new repo for this step.

Step2 Initialize hexo

This step is standalone with github deployment.

1
2
3
$ hexo init blog
$ cd blog
$ npm install

Step3 Connecting to remote github repo

1
2
3
4
5
6
7
$ cd blog
$ git init
$ git checkout -b source
$ git add .
$ git commit -m "int blog infra"
$ git remote add origin https://github.com/username/username.github.io.git
$ git push -u origin souce

Step4 Configure sub-branch for deployment

1
2
3
4
5
6
$ git checkout --orphan gh-pages
$ git rm -rf .
$ echo "Hello GitHub Pages" > index.html
$ git add .
$ git commit -m "Init gh-pages"
$ git push origin gh-pages

Step5 Commit code and deployment

  1. Go to repo -> Settings -> Pages -> set branch to gh-pages & /root
  2. Can commit code in source branch
  3. Can deploy blog in gh pages triggered by Github Action
Author: Yu
Link: https://yurihe.github.io/2025/03/29/1.build_blog/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.