环境
MacBook Pro
Git:2.21
Huxo:v0.62.2
安装Hugo
① MacBook 推荐使用:
brew install hugo
② 其他环境,稍微麻烦些:需要安装Go 1.3+ (Go 1.4+ on Windows)、Mercurial和 Git。
设置好 GOPATH
环境变量,获取源码并编译:
$ export GOPATH=$HOME/go
$ go get -v github.com/spf13/hugo
源码会下载到 $GOPATH/src
目录,二进制在 $GOPATH/bin/
如果需要更新所有Hugo的依赖库,增加 -u
参数:
$ go get -u -v github.com/spf13/hugo
具体可以查看Hugo
中文官网
验证安装
hugo version
创建站点 – 博客根目录
# 执行命令 hugo new site myhugoblog
yutaodeMacBook-Pro:hugoblog yutao$ hugo new site myhugoblog
Congratulations! Your new Hugo site is created in /Users/yutao/myproject/hugoblog/myhugoblog.
Just a few more steps and you're ready to go:
1. Download a theme into the same-named folder.
Choose a theme from https://themes.gohugo.io/ or
create your own with the "hugo new theme <THEMENAME>" command.
2. Perhaps you want to add some content. You can add single files
with "hugo new <SECTIONNAME>/<FILENAME>.<FORMAT>".
3. Start the built-in live server via "hugo server".
Visit https://gohugo.io/ for quickstart guide and full documentation.
添加主题
先 下载 主题
yutaodeMacBook-Pro:myhugoblog yutao$ git init
Initialized empty Git repository in /Users/yutao/myproject/hugoblog/myhugoblog/.git/
# 执行命令 git submodule add git@github.com:flysnow-org/maupassant-hugo.git themes/maupassant
yutaodeMacBook-Pro:myhugoblog yutao$ git submodule add git@github.com:flysnow-org/maupassant-hugo.git themes/maupassant
Cloning into '/Users/yutao/myproject/hugoblog/myhugoblog/themes/maupassant'...
remote: Enumerating objects: 9, done.
remote: Counting objects: 100% (9/9), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 638 (delta 1), reused 5 (delta 1), pack-reused 629
Receiving objects: 100% (638/638), 1001.13 KiB | 163.00 KiB/s, done.
Resolving deltas: 100% (355/355), done.
将主题名称写入配置:
# 执行命令:echo 'theme = "maupassant" >> config.toml
yutaodeMacBook-Pro:myhugoblog yutao$ echo 'theme = "maupassant" >> config.toml
写文章
① 使用Hugo
命令生成md
文件。
hugo new posts/my-first-post.md
文件内容:
---
title: "My First Post"
date: 2019-03-26T08:47:11+01:00
draft: true
---
其中draft
是草稿的意思。设置为true
是不会部署的。所以部署时,要设置false
。
② 正常情况下,我觉得大家都是用自己喜爱的编辑器来写的吧,比如我是Typora
,所以怎么创建md
文件是次要的;
这里需要注意的地方是:文章(文件),得放到
content/posts/
这个目录下。
本地启动Hugo服务
# 执行命令 hugo server -D
yutaodeMacBook-Pro:myhugoblog yutao$ hugo server -D
| EN
+------------------+----+
Pages | 10
Paginator pages | 0
Non-page files | 0
Static files | 5
Processed images | 0
Aliases | 2
Sitemaps | 1
Cleaned | 0
Built in 24 ms
Watching for changes in /Users/yutao/myproject/hugoblog/myhugoblog/{archetypes,content,data,layouts,static,themes}
Watching for config changes in /Users/yutao/myproject/hugoblog/myhugoblog/config.toml
Environment: "development"
Serving pages from memory
Running in Fast Render Mode. For full rebuilds on change: hugo server --disableFastRender
Web Server is available at http://localhost:1313/ (bind address 127.0.0.1)
Press Ctrl+C to stop
然后浏览器访问:http://localhost:1313 即可。
自定义主题
主题可以自由更换,只需要把主题的代码下载到themes目录下就可。
建议使用命令:
git submodule add [url] themes/主题名
例如:
git submodule add https://github.com/budparr/gohugo-theme-ananke.git themes/ananke
项目配置文件
config.toml
是项目的配置文件。
打开后可以看到如下:
# 部署github pages时,这个要修改
baseURL = "https://example.org/"
languageCode = "en-us"
title = "My New Hugo Site"
# 换主题时,更换这个名称
theme = "ananke"
假设要部署到github pages中时,baseURL地址得更换,本地运行不需要管。
构建静态页面
# 构建项目
hugo -D
# 或者
hugo
其会在根目录下,生成public
目录,静态html
及资源文件都在这里面。
部署
修改配置文件config.toml
:
我修改后的文件:
#baseURL = "http://example.org/"
baseURL = "http://b3601993.github.io/"
languageCode = "en-us"
title = "My New Hugo Site"
theme = "maupassant"
再执行命令:
hugo
生成静态文件。
接下来会hexo
是不同的,hexo
是执行命令hexo -d
来进行部署。
而Hugo
呢,而是常规的git push
提交代码。
如何创建Github Pages,我这里就不讲解了,
原理就是创建一个[github用户名.github.io]这样一个仓库;
然后往这里仓库放生成好的静态文件html\css等。
对了码云也有,但是建议先玩GitHub的pages再去玩码云的。
假设GitHub.io仓库是新创建的
这个时候只需要安装官网教程来执行就可以了:
$ cd public
$ git init
# 换成自己
$ git remote add origin https://github.com/coderzh/coderzh.github.io.git
$ git add -A
$ git commit -m "first commit"
$ git push -u origin master
假设GitHub.io仓库早就有了
这个时候,仓库里面已经有代码了:
先将代码拉下来(随便哪个目录),再执行如下三个命令:
yutaodeMacBook-Pro:b3601993.github.io yutao$ git rm -rf --cached *
yutaodeMacBook-Pro:b3601993.github.io yutao$ git commit -am "删除"
yutaodeMacBook-Pro:b3601993.github.io yutao$ git push
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 4 threads
Compressing objects: 100% (1/1), done.
Writing objects: 100% (2/2), 195 bytes | 195.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To github.com:b3601993/b3601993.github.io.git
347d3d5..dc5624b master -> master
上面只是清楚掉了原有的代码。
再回到public
目录执行:
$ cd public
$ git init
# 换成自己
$ git remote add origin https://github.com/coderzh/coderzh.github.io.git
$ git add -A
$ git commit -m "first commit"
这里不再执行git push
,而是执行:
yutaodeMacBook-Pro:public yutao$ git pull origin master --allow-unrelated-histories
之后再,执行:
yutaodeMacBook-Pro:public yutao$ git push origin --set-upstream master
Enumerating objects: 25, done.
Counting objects: 100% (25/25), done.
Delta compression using up to 4 threads
Compressing objects: 100% (21/21), done.
Writing objects: 100% (24/24), 21.37 KiB | 4.27 MiB/s, done.
Total 24 (delta 6), reused 0 (delta 0)
remote: Resolving deltas: 100% (6/6), done.
就可以了。
坑
我很多文章都是在CSDN上,300篇左右,那种文章都没有如下标题和时间信息的
---
title: "hugo部署到gitee pages"
date: 2020-01-09T18:39:07+08:00
---
我总不能一一的去添加吧,以前因为这个放弃了hexo
,看来我又要放弃Hugo
了。-- 今天刚弄
参考地址:
https://gohugo.io/getting-started/quick-start/
https://www.gohugo.org/
https://www.flysnow.org/2018/07/29/from-hexo-to-hugo.html
https://www.educative.io/edpresso/the-fatal-refusing-to-merge-unrelated-histories-git-error