Hexo博客部署到GitHub Pages的说明

要将Hexo博客部署到GitHub Pages,可以按照以下步骤操作:


1. 准备工作

  • 安装 Hexo(如果尚未安装):
    1
    npm install -g hexo-cli
  • 创建 Hexo 项目(如果还没有):
    1
    2
    3
    hexo init my-blog
    cd my-blog
    npm install
  • 确保 Git 已安装
    1
    git --version
    如果没有,请先安装 Git

2. 创建 GitHub 仓库

  1. 在 GitHub 上新建仓库

    • 仓库名格式必须为:<你的用户名>.github.io(例如 yourusername.github.io)。
    • 如果是项目站点(非个人主页),可以任意命名,但需在 _config.yml 中额外配置。
  2. 初始化本地 Git 并关联远程仓库

    1
    2
    git init
    git remote add origin https://github.com/yourusername/yourusername.github.io.git

3. 安装 Hexo 部署插件

1
npm install hexo-deployer-git --save

4. 配置 _config.yml

在 Hexo 根目录下的 _config.yml 文件末尾找到 deploy 部分,修改为:

1
2
3
4
5
deploy:
type: git
repo: https://github.com/yourusername/yourusername.github.io.git
branch: main # 或 master(GitHub 默认分支)
message: "更新博客内容" # 可选,部署时的提交信息

注意

  • 如果使用 SSH 方式,repo 可写为 git@github.com:yourusername/yourusername.github.io.git
  • 确保缩进正确(2 个空格)。

5. 生成静态文件并部署

  1. 生成静态文件

    1
    hexo clean && hexo generate

    (或简写 hexo clean && hexo g

  2. 部署到 GitHub

    1
    hexo deploy

    (或简写 hexo d

  3. 访问博客
    稍等几分钟后,打开 https://yourusername.github.io 即可查看。


6. 可选:自定义域名

  1. 在仓库的 Settings > Pages 里设置自定义域名(如 blog.yourdomain.com)。
  2. 在 Hexo 的 source 目录下创建 CNAME 文件(无后缀):
    1
    blog.yourdomain.com
  3. 重新部署:
    1
    hexo clean && hexo g -d

7. 管理博客源码(推荐)

为了避免丢失本地文件,建议将 Hexo 源码也推送到 GitHub:

  1. 新建一个分支(如 hexo-source
    1
    git checkout -b hexo-source
  2. 提交代码
    1
    2
    3
    git add .
    git commit -m "初始化 Hexo 源码"
    git push origin hexo-source
  3. 以后更新博客时,先提交源码,再部署:
    1
    2
    3
    4
    git add .
    git commit -m "更新文章"
    git push origin hexo-source
    hexo clean && hexo g -d

常见问题

1. 部署后页面 404?

  • 检查仓库名是否正确(必须是 <username>.github.io)。
  • 确保 branchmainmaster(与 GitHub 默认分支一致)。

2. 部署失败(权限错误)?

  • 如果使用 SSH,确保已添加公钥到 GitHub。
  • 如果使用 HTTPS,可能需要输入 GitHub 账号密码(推荐改用 SSHPersonal Access Token)。

3. 如何更新博客?

  • 修改文章后,重新执行:
    1
    hexo clean && hexo g -d


Hexo博客部署到GitHub Pages的说明
hhttps://www.fengyu-blog.top/2025/05/10/Hexo博客部署到GitHub Pages的说明/
作者
风语
发布于
2025年5月10日
许可协议