我的 Git 提交规范:让 git log 变成项目文档

Alex Chen | 2026-09-11T14:41:00 | DevOps

好的 commit message 就是最好的项目文档。分享一下我们团队的 Git 提交规范和自动化检查工具。

我一直觉得 `git log` 应该是项目最好的变更记录。但现实是大部分项目的 git log 长这样: ``` fix bug update wip asdfasdf fix fix again ``` 看了等于没看。后来我们团队引入了 Conventional Commits 规范,效果很好。 ## 规范格式 ``` (): ``` ### type 类型 - `feat`: 新功能 - `fix`: 修复 bug - `refactor`: 重构(不改功能) - `perf`: 性能优化 - `docs`: 文档 - `test`: 测试 - `chore`: 构建/工具变更 ### 好的例子 ``` feat(auth): add JWT refresh token support Implement automatic token refresh when the access token expires. The refresh token is stored in httpOnly cookie for security. Closes #142 ``` ``` fix(order): prevent duplicate order submission Add idempotency token check before processing order creation. Previously, rapid double-clicks could create duplicate orders. ``` ## 自动化检查 用 commitlint + husky 在提交时自动检查格式: ```bash npm install -D @commitlint/cli @commitlint/config-conventional husky # commitlint.config.js module.exports = { extends: ['@commitlint/config-conventional'] }; # 安装 husky npx husky init echo "npx commitlint --edit \$1" > .husky/commit-msg ``` 不符合规范的提交会被直接拒绝。刚开始大家觉得烦,用了一个月之后都觉得真香。 ## 自动生成 CHANGELOG 规范化的 commit message 可以用 `standard-version` 或 `release-please` 自动生成 CHANGELOG: ```bash npx standard-version # 自动根据 feat/fix 类型生成: # ## Features # * **auth**: add JWT refresh token support # ## Bug Fixes # * **order**: prevent duplicate order submission ``` ## 总结 好的 commit message 就三个要求:说清楚改了什么(type + scope)、为什么改(body)、关联了什么(footer)。配合自动化工具强制执行,一两周就能养成习惯。

← Back to Blog