Git Commit规范指北
参考文章
背景
Git 每次提交代码,都要写 (提交说明),提交内容也五花八门,不便于后续的项目阅读和管理。
优点
git blame => 查看文件的每一行是谁修改的
如下:查看 index.html中10行2列修改人
git blame -L 10,+2 index.html
规范
可以使用典型的Git工作流程或通过使用CLI向导来添加提交消息格式。
使用界面:
安装
推荐全局安装,项目中配置即可
# 全局安装
pnpm install -g commitizen cz-conventional-changelog
配置
在文件中,添加以下代码:
"config": {"commitizen": {"path": "cz-conventional-changelog"}
}
使用
凡是用到git 命令,一律改为使用git cz。
此时会出现这样的交互式命令行,根据提示完成提交
? Select the type of change that you're committing: (Use arrow keys)
❯ feat: A new feature fix: A bug fix improvement: An improvement to a current feature docs: Documentation only changes style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc) refactor: A code change that neither fixes a bug nor adds a feature perf: A code change that improves performance
(Move up and down to reveal more choices)
使用
凡是用到git 命令,一律改为使用git cz。这时,就会出现选项,用来生成符合格式的 。
编辑器插件
如果你使用的是,在中搜索安装即可
到此就可以使用 git cz 来替换git 了,
最后给你项目的加上一个标识吧。
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
格式
每次提交, 都包括三个部分:,body 和 。
():
部分只有一行,包括三个字段:type(必需)、scope(可选)和(必需)。
用于说明 的类别,只允许使用下面7个标识。
类别 - 说明
feat: | 新功能(feature)fix: | 修补bugdocs: | 文档(documentation)style: | 格式(不影响代码运行的变动)refactor:| 重构(即不是新增功能,也不是修改bug的代码变动)test: | 增加测试chore: | 构建过程或辅助工具的变动 revert: | commit 回退
scope用于说明 影响的范围,比如数据层、控制层、视图层等等,视项目不同而不同。
是 目的的简短描述,不超过50个字符。
Body
Body 部分是对本次 的详细描述,可以分成多行。下面是一个范例。
部分只用于以下两种情况:
不兼容变动
关闭 Issue
规范校验 安装
husky
通过特定的 git hook 来检查git提交信息,代码lint等功能
npm install --save-dev husky
pnpm install -D @commitlint/cli @commitlint/config-conventional
添加配置
.json添加如下配置
"commitlint": {"extends": ["@commitlint/config-conventional"]
}
使用 husky 命令在,创建 -msg 钩子,并执行验证命令:
npx husky add .husky/commit-msg "npx --no-install commitlint --edit $1"
添加标识
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
无权问题
chmod ug+x .husky/*
chmod ug+x .git/hooks/*
自动版本管理和生成
版本管理和
完整的.json
+ cz-- + +-
{"name": "blog","version": "1.0.0","description": "blog","main": "index.js","scripts": {"start": "npm run dev","release": "standard-version","release:alpha": "standard-version --prerelease alpha","release:rc": "standard-version --prerelease rc","release:major": "npm run release -- --release-as major","release:minor": "npm run release -- --release-as minor","release:patch": "npm run release -- --release-as patch"},"devDependencies": {"@commitlint/cli": "^17.6.6","@commitlint/config-conventional": "^17.6.6","commitizen": "^4.2.2","cz-conventional-changelog": "^3.3.0","standard-version": "^9.1.0"},"config": {"commitizen": {"path": "cz-conventional-changelog"}},"commitlint": {"extends": ["@commitlint/config-conventional"]}
}