使用Github Pages搭建博客

本文最后更新于:2025年1月29日 凌晨

Github Pages 建站教程

电脑中已安装:

  • Node.js (Node.js 版本需不低于 10.13,建议使用 Node.js 12.0 及以上版本)
  • Git

在所需的应用程序安装完成后,即可使用 npm 安装 hexo

1
npm install -g hexo-cli

安装完 hexo 后,执行下列命令

1
2
3
hexo init <folder>
cd <folder>
npm install

会生成下列所示的文件目录

1
2
3
4
5
6
7
8
.
├── _config.yml # 修改网站的配置信息
├── package.json
├── scaffolds # 存储模板文件,当您新建文章时,Hexo 会根据 scaffold 来建立文件
├── source
| ├── _drafts
| └── _posts
└── themes

该写点啥了

运行命令,生成要转为 html 文件的 md 文件。

1
hexo new [layout] <title>

eg. hexo new "post title with whitespace" 会生成默认布局的以 post title with whitespace 为标题的 md 文件。

运行命令,生成静态文件,即要渲染到 Github Pages 的网页文件.

1
hexo generate 

运行命令,启动服务器,查看效果。默认情况下,访问网址为: http://localhost:4000/。每次修改后,都要重新运行命令,查看更新效果。

1
hexo server

最后一步,部署到 Github Pages

首先安装 hexo-deployer-git

1
npm install hexo-deployer-git --save	

再修改 _config.yml 的配置

1
2
3
4
5
deploy:
type: git
# repo: <repository url> #https://bitbucket.org/JohnSmith/johnsmith.bitbucket.io
repo: git@github.com:Hercules11/Hercules11.github.io.git # 走 ssh 通道, 不用输入用户名、密码
branch: [branch] # 页面文件所在分支

Hexo 部署的原理

当执行 hexo deploy 时,Hexo 会将 public 目录中的文件和目录推送至 _config.yml 中指定的远端仓库和分支中,并且完全覆盖该分支下的已有内容。

自动化部署

参考: 个人博客搭建指南


使用Github Pages搭建博客
https://hercules11.github.io/blog/2021/06/21/use-github-pages-build-blog/
作者
wxc
发布于
2021年6月21日
许可协议