使用hexo搭建github pages

使用hexo搭建github pages。

准备工作

  1. git一些基本知识,可以看 廖雪峰的Git教程
  2. 一个github账号

github pages

只需要新建一个repository,名为 {username}.github.io,username为你注册的github账号,然后新建一个页面index.html,写入hello,提交到github中,在页面{username}.github.io中即可显示页面内容。

安装hexo

Requirements

安装hexo

1
$ npm install -g hexo-cli

初始化

1
2
3
4
$ npm install hexo --save
$ hexo init <folder>
$ cd <folder>
$ npm install

Hexo命令

新建一篇文章

1
$ hexo new "My New Post"

More info: Writing

运行server

1
$ hexo server ## 可以简写为 hexo s

可以在浏览器中查看(http://localhost:4000)

More info: Server

生成静态页面

1
$ hexo generate ## 一般hexo会自动生成

More info: Generating

自动部署到github上

1
$ hexo deploy ## 使用hexo d -g

要使用此命令,首先得安装git部署插件

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

然后在_config.yml中添加如下设置:

1
2
3
4
5
deploy:
type: git
repo: <repository url>
branch: [branch] ##可省略
message: [message] ##可省略

More info: Deployment

如何处处写文章

  1. 安装git, node.js
  2. 安装hexo, npm install -g hexo-cli
  3. 从github上拉代码, git clone https://github.com/wzktravel/hexo.git
  4. 初始化hexo
1
2
3
4
$ npm install hexo --save
$ hexo init <folder> ##取消此句!!已经有hexo结构,无需init
$ cd <folder>
$ npm install
  1. 文章md源码在source/_posts/下,使用hexo new ${post}新建文章,使用hexo s启动服务,hexo d -g提交到github上
  2. 注意最后将文章源码也提交到线上,git add *, git commit -m "${comment}", git push origin master

参考