跳过正文
  1. 技术/

搭建个人博客——3.GitHub Pages 发布博客

2 分钟· ·
Yuzhen 配置 文档
Yuzhen
作者
Yuzhen
好奇驱动,积极成长。
目录
个人博客 部署教程 - 这篇文章属于一个选集。
§ 3: 本文

我们希望 Hugo 生成的静态网站能通过 GitHub Pages 服务进行托管,而无需自己维护服务,更稳定、安全,因此我们需要上传 Hugo 生成的静态网页文件至 GitHub Page 项目仓库。

1. 准备 GitHub 仓库
#

  1. 登录后,点击右上角,出现下拉菜单,点击 Your repositories 进入页面

  2. 点击 New

  3. 进入 Creat a new repository 页面

  4. 创建两个新的仓库,一个命名为 username.github.io,用来托管public的站点, username 替换为你的 GitHub 用户名。另外创建一个,可以叫my_site_source的private仓库。通常情况下创建一个就可以,my_site_source的目的是存储原始文件,作为容灾备份。

    例如,如果你的 GitHub 用户名是 yuzhencode,则仓库名为 yuzhencode.github.io。

  5. 初始化仓库时,勾选 Initialize this repository with a README,可以方便后续推送代码。


2. 本地生成 Hugo 静态网页
#

  1. 确保你的 hugo.toml 或配置文件中有正确的 baseURL,例如
baseURL = "https://username.github.io/"
# 将 `username` 替换为你的 GitHub 用户名。
  1. 生成静态网页
  • 打开你的博客项目目录,运行以下命令生成静态网页:
$hugo
  • 生成的静态文件将位于项目的 public/ 文件夹中。

3. 将生成的文件上传到 GitHub 仓库
#

  1. 初始化 Git
  • 进入生成的 public/ 文件夹,运行:

    $cd public
    $git init
    
  1. 添加 GitHub 仓库为远程仓库
  • 将 GitHub 仓库添加为远程仓库:

    $git remote add origin https://github.com/yuzhencode/yuzhencode.github.io.git
    # 将本地目录链接到远程服务器的代码仓库
    
  1. 添加 public/ 目录的内容到 Git
  • 添加文件:

    $git add .
    $git commit -m "Initial commit - Hugo static files"
    
  1. 推送到 GitHub
  • 将本地的 public/ 文件推送到 GitHub 仓库:

    $git branch -M main
    $git push -u origin main
    

4. 配置 GitHub Pages
#

  1. 启用 GitHub Pages
  • 在 GitHub 项目的 Settings 页面的左侧菜单中找到 Pages
  • Source 下拉框中,选择 main 分支,并指定 /(root) 作为发布目录。
  • 保存设置后,GitHub 将开始构建并托管你的 Hugo 博客。
  1. 访问你的博客
  • GitHub Pages 部署完成后,你可以通过 https://your-username.github.io/ 访问你的博客。

5. 本地更新与部署
#

每次在 Hugo 中更新内容后,重新生成 public/ 文件,并重复步骤 4 来推送更新:

$hugo
$cd public
$git add .
$git commit -m "Update site"
$git push -u origin main

这样,你的 Hugo 静态网站就会保持同步,并成功部署到 GitHub Pages 上。


6.容灾备份
#

像步骤3执行同样的命令,不过在站点根目录或上一层建立一个my-site-source目录下进行。

  1. 初始化 Git
  • 运行:

    $git init
    
  1. 添加 GitHub 仓库为远程仓库
$git remote add origin https://github.com/yuzhencode/yuzhenblog-source.git
# 将本地目录链接到远程服务器的代码仓库
  1. 添加文件,推送到 GitHub 仓库
$git add .
$git commit -m "Initial commit - Hugo static files"
$git push -u origin main
  1. 定期更新
$git add .
$git commit -m "Update site"
$git push -u origin main

至此,Hugo + Blowfish + GitHub 支持下的博客就大功告成啦🎉

个人博客 部署教程 - 这篇文章属于一个选集。
§ 3: 本文

相关文章

搭建个人博客——2. Blowfish主题自定义
4 分钟
Yuzhen 配置 文档
搭建个人博客——1. 安装hugo, Git, 配置Blowfish主题
3 分钟
Yuzhen 配置 文档
关于我
1 分钟
关于我 Yuzhen