Title here
Summary here
2026年8月21日 教程2 min
在使用 Hugo 搭建博客并部署到 Cloudflare Pages 时,正确的 Git 提交流程很重要。本文记录了如何避免提交不必要的构建文件,保持仓库整洁。
Hugo 会将源文件(content/)编译成静态 HTML 文件输出到 public/ 目录。这些构建产物不应该提交到 Git 仓库,因为:
首先创建 .gitignore 文件,排除构建输出和临时文件:
# Hugo build output
public/
resources/
# Hugo lock file
.hugo_build.lock
# IDE
.vscode/
.idea/
*.swp
*.swo
*~
# OS
.DS_Store
Thumbs.db
# Node modules (if using npm for theme development)
node_modules/
# Temporary files
*.log
*.tmp
# 创建新文章
hugo new posts/my-new-post.md
# 或直接编辑现有文章
# vim content/posts/existing-post.md# 启动开发服务器,-D 参数显示草稿
hugo server -D
# 浏览器访问 http://localhost:1313 预览效果
# 确认无误后 Ctrl+C 停止服务器# 查看修改了哪些文件
git status
# 添加要提交的文件
git add content/posts/my-new-post.md
# 或一次添加所有修改(.gitignore 会自动排除构建文件)
git add .
# 提交,使用清晰的提交信息
git commit -m "feat: 添加新文章《文章标题》"
# 推送到远程仓库
git push origin main推送后,Cloudflare Pages 会自动:
hugo 构建命令建议使用语义化的提交信息,让历史记录更清晰:
| 前缀 | 用途 | 示例 |
|---|---|---|
feat: |
新增文章或功能 | feat: 添加 Docker 入门教程 |
fix: |
修正内容错误 | fix: 修正文章中的代码错误 |
docs: |
文档更新 | docs: 更新 README 说明 |
style: |
样式或格式调整 | style: 优化代码高亮配置 |
chore: |
日常维护 | chore: 更新依赖版本 |
✅ 应该提交:
content/ - 所有 Markdown 文章和页面hugo.toml - Hugo 配置文件static/ - 静态资源(图片、自定义 CSS/JS)layouts/ - 自定义布局模板archetypes/ - 文章模板themes/ - 主题文件(如果自己管理).gitignore - Git 忽略规则❌ 不应该提交:
public/ - Hugo 构建输出目录resources/ - Hugo 资源缓存.hugo_build.lock - Hugo 锁文件.DS_Store - macOS 系统文件.vscode/ - 编辑器配置# 从 Git 跟踪中移除(但保留本地文件)
git rm -r --cached public/
# 提交这个改动
git commit -m "chore: 从版本控制中移除 public 目录"
# 推送到远程
git push origin main# 查看 Git 状态,确认 public/ 没有出现
git status
# 或者检查是否被忽略
git check-ignore public/git add . 吗?可以。只要 .gitignore 配置正确,git add . 会自动排除被忽略的文件,不会添加 public/ 等目录。
从创建文章到发布的完整流程:
# 1. 创建新文章
hugo new posts/docker-tutorial.md
# 2. 编辑文章内容
# 使用你喜欢的编辑器编辑 content/posts/docker-tutorial.md
# 3. 本地预览
hugo server -D
# 在浏览器中检查效果
# 4. 修改文章头部,将 draft 改为 false
# draft = false
# 5. 提交到 Git
git add content/posts/docker-tutorial.md
git commit -m "feat: 添加 Docker 基础教程"
git push origin main
# 6. 等待 Cloudflare Pages 自动部署
# 几分钟后访问网站即可看到新文章使用 Hugo + Git + Cloudflare Pages 的工作流程核心原则:
这样可以保持 Git 仓库整洁,专注于内容创作,而不用担心构建和部署的细节。