git commit 代码提交规范

Conventional Commits 约定式提交

  • 统一团队 Git Commit 标准,便于后续代码 review、版本发布、自动化生成 change log 或 release note
  • 可以提供更多更有效的历史信息,方便快速预览以及配合 cherry-pick 快速合并代码
  • 团队其他成员进行类 git blame 时可以快速明白代码用意

校验辅助工具

husky: supports all Git hooks, can lint your commit messages, run tests, lint code, etc... commitlint: Lint commit messages


# 安装husky和commitlint
$ pnpm add -D husky @commitlint/config-conventional @commitlint/cli

# warning: 以下命令适用于husky8
# 安装husky提供的git hook:package.json > prepare script 的prepare脚本会在执行npm install之后自动执行
$ npm set-script prepare "husky install"
$ npm run prepare

# git hook(pre-commit):提交git commit命令前触发,脚本内容自定义,一般添加eslint
$ npx husky add .husky/pre-commit "xxx"

# git hook(commit-msg):规范 commit message 信息,需要配置commitlint
$ npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'
$ echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js

配置 commitlint 时 Windows 可能出现的问题:

  • 文件编码格式应该为 UTF-8
  • package.json 中的 type 属性配置:commitlint.config.js -> "commonjs",commitlint.config.cjs -> "module"
  • @commitlint/config-conventional 类型默认包括:[build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test]

commit 说明

例子:

feat(cli): create initial commit
fix: feat(0429 留言下单): add 'graphiteWidth' option

规范: type(scope): subject

type:用于说明 commit 的类别,规定为如下几种:

feat:新增功能(feature)
fix:修复 bug
style:仅仅修改了空格、缩进等,不改变代码逻辑
refactor:代码重构,未新增任何功能和修复任何 bug
chore:非 src 和 test 的修改构建过程或辅助工具的变动(杂项)
docs:修改文档(documentation)
perf:改善性能和体现的修改
test:测试用例的修改
ci:自动化流程配置修改
revert:回滚到上一个版本
build:改变构建流程,新增依赖库、工具等(例如 webpack 修改)(可选)

scope:【可选】用于说明 commit 的影响范围,比如: route, component, utils, build...

subject:commit 的简要说明,不超过 50 个字符,以动词开头,使用第一人称现在时,比如 change,而不是 changed 或 changes 第一个字母小写结尾不加句号(.)

如果修改范围以上都应用不了,可能就是 commit 合并太多

Copyright © Hazel Wei all rights reserved, powered by Gitbook            该文章修订时间: 2025-06-30 22:12:52

results matching ""

    No results matching ""