git commit规范,代码提交工具推荐

目录

一、代码提交风格

1.安装Commitizen

2.安装cz-conventional-changelog,并且初始化cz-conventional-changelog:

3.这个时候我们提交代码需要使用 npx cz:  

第一步是选择type,本次更新的类型

第二步选择本次修改的范围(作用域)

  第三步选择提交的信息

 第四步提交详细的描述信息

第五步是否是一次重大的更改

第六步是否影响某个open issue

4、可以通过git log来查看本次提交

二、代码提交验证 

1.安装 @commitlint/config-conventional 和 @commitlint/cli

2.在根目录创建commitlint.config.js文件,配置commitlint

3.使用husky生成commit-msg文件,验证提交信息:

4、修改package.json配置,使用npm run commit提交更方便


一、代码提交风格

通常我们的git commit会按照统一的风格来提交,这样可以快速定位每次提交的内容,方便之后对版本进行控制。

但是如果每次手动来编写这些是比较麻烦的事情,我们可以使用一个工具:Commitizen

Commitizen 是一个帮助我们编写规范 commit message 的工具;

1.安装Commitizen
npm install commitizen -D
2.安装cz-conventional-changelog,并且初始化cz-conventional-changelog:
npx commitizen init cz-conventional-changelog --save-dev --save-exact

这个命令会帮助我们安装cz-conventional-changelog:

并且在package.json中进行配置:

3.这个时候我们提交代码需要使用 npx cz:  
npx cz

回车后:

  • 第一步是选择type,本次更新的类型
Type 作用
feat 新增特性 (feature)
fix 修复 Bug(bug fix)
docs 修改文档 (documentation)
style 代码格式修改(white-space, formatting, missing semi colons, etc)
refactor 代码重构(refactor)
perf 改善性能(A code change that improves performance)
test 测试(when adding missing tests)
build 变更项目构建或外部依赖(例如 scopes: webpack、gulp、npm 等)
ci 更改持续集成软件的配置文件和 package 中的 scripts 命令,例如 scopes: Travis, Circle 等
chore 变更构建流程或辅助工具(比如更改测试环境)
revert 代码回退
  • 第二步选择本次修改的范围(作用域)

? What is the scope of this change (e.g. component or file name): (press enter to skip)

这次修改所影响的范围

  •   第三步选择提交的信息

 ? Write a short, imperative tense description of the change (max 86 chars):

提交描述的具体信息

  •  第四步提交详细的描述信息

 ? Provide a longer description of the change: (press enter to skip)

是否提交详细的描述信息

  • 第五步是否是一次重大的更改

 ? Are there any breaking changes?

是否是一次重大的更改(默认no)

  • 第六步是否影响某个open issue

? Does this change affect any open issues?  

是否影响某个open issue(一般在开源项目中,公司里一般no)

4、可以通过git log来查看本次提交
git log

二、代码提交验证 

如果我们按照cz来规范了提交风格,但是依然有同事通过 git commit 按照不规范的格式提交应该怎么办呢?

  • 我们可以通过commitlint来限制提交;

1.安装 @commitlint/config-conventional 和 @commitlint/cli
npm i @commitlint/config-conventional @commitlint/cli -D
2.在根目录创建commitlint.config.js文件,配置commitlint
module.exports = {
  extends: ['@commitlint/config-conventional']
}
3.使用husky生成commit-msg文件,验证提交信息:
npx husky add .husky/commit-msg "npx --no-install commitlint --edit $1"
4、修改package.json配置,使用npm run commit提交更方便

npm run commit

猜你喜欢

转载自blog.csdn.net/m0_60820509/article/details/132758706