提示:教程默认你已经安装了 git,hugo 安装参考官网:
git地址:https://git-scm.com/book/zh/v2/起步-安装-Git-Git
hugo地址:https://gohugo.io/getting-started/installing/

mac用户可以使用Homebrew安装

brew install git:
brew install hugo

下面会分四步介绍

一.配置github

1.注册账号 GitHub地址:https://github.com/
2.新建博客发布仓库
这里建议新建2个仓库,和fork一个主题
一个是用于发布我们博文用的仓库,一个是我们存放源码仓库,fork的主题是方便以后修改样式(可做可不做)
新建 uunao.github.io
https://github.com/uunao/uunao.github.io.git
Settings –>GitHub Pages

3.新建博客源代码
myblog https://github.com/uunao/myblog.git

4.fork我们相中的主题到本地,方便以后自己修改
这个我的博客模板,用的飞雪无情的:https://github.com/uunao/maupassant-hugo.git

也可以到官网或者网上搜索大神们写的主题,保存到自己的github上。
hugo主题地址:https://themes.gohugo.io/

二.配置本地

1.新建站点

hugo new site myblog    

2.新建博客目录

cd myblog   

3.初花git

git init    

4.clone 主题为子模块

git submodule add https://github.com/uunao/maupassant-hugo.git themes/maupassant    

5.复制主题配置文件

cp themes/maupassant/exampleSite/config.toml .  

6.编辑配置文件

vim config.toml

baseURL = "uunao.github.io" #我们申请的github发布仓库地址
languageCode = "zh-CN"
title = "小九的博客"       #博客的名字
theme = "maupassant"     #主题名称 -无需修改

三.发布

1.预览效果
hugo server http://localhost:1313

2.发布博文
把博客仓库已子模块的形式添加到public下
git submodule add https://github.com/uunao/uunao.github.io.git public
执行 hugo 命令
默认会在public下生成很多编译好的文件

git add *
git commit -m "init"
git push  

3.最后把源码提交到myblog仓库备份

cd myblog   
git remote add origin https://github.com/uunao/blog.git 
git add .   
git commint -m "init"  #init 是每次提交的备注,可自定义 
git checkout main   
git branch --set-upstream-to=origin/main main   

四.写文章并发布

1.新增博文 –默认会生成到content目录下面 (如果报错就cd到上级目录)
hugo new content/post/my-first-blog.md

2.编辑文件,把草稿状态改成false。文章才能显示

vim content/post/my-first-blog.md 

title: "My First Blog"
date: 2021-04-21T14:28:08+08:00
draft: false   #需要改成false

3.预览效果 hugo server

4.生成最终需要发布的页面 hugo

5.提交到博客仓库

cd public #进入myblo/public目录下
git add .
git commit -m "my first blog"
git push

6.提交源码到源码仓库

cd ..  #定位到myblog目录下
git add .
git commit -m "my first blog"
git push

五.git提交异常

git push

根据提示执行,如果又报如下图错误 
git push --set-upstream origin main 

先执行
git pull --rebase origin master  #远程库中的更新合并到本地库中,然后在把刚刚提交的合并在一起

再执行上面的命令就不报错了
git push --set-upstream origin main  #把本地分支合并到main上