1.通常我们提交代码是这样提交的的,如下面例子这样,这样的习惯是很不好的,我们最好是当前做了了什么操作,修改哪些东西备注说明
git commit -m 'test01' //之前
git commit -m "项目搭建(修改登录窗体)"
2.我们现在主要目的是让我们的代码提交信息尽可能的规范,通常我们的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 :
//这次提交的类型是什么 ? Select the type of change that you're committing: (Use arrow keys) ❯ feat: A new feature //新增特性(feature) fix: A bug fix //修复bug(bug fix) 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 //改善性能 test: Adding missing tests or correcting existing tests //测试 //变更项目构建或外部依赖(例如 scopes:webpack、gulp、npm等) build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm) //更改持续集成软件的配置文件和package中的script 命令 ❯ ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs ) chore: Other changes that don't modify src or test files //变更构建流程或辅助工具(比如更改测试环境) revert: Reverts a previous commit //代码回退
第一步:选择type,本次更新的类型
第二步:选择本次修改影响的范围(作用域):假如这里影响的是登录模块
第三步:选择提交的信息 (给本次提交写上对应的描述)
第四步:可额外写更多的描述,直接跳过也可以
第五步:这次提交是不是一次的大的更新
第六步:会不会影响到开源项目 ,(回车忽略 )
第七步:提示,告诉我们这些项目还没有放到我们的暂缓区里面
第八步:git add . 提交暂缓区里面
第九步: npx cz
第十步:查看本次提交信息:
小注意:clear 命令 可清除之前的打印信息。
代码提交验证
如果我们按照cz 来规范了提交代码风格,但是依然有同事通过 git commit 按照不规范的格式提交应该怎么办呢?
- 我们可以通过commitint 来限制提交;
1.安装@commitlint/config-conventional和 @commitlint/cli
npm install @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 commitlin --edit $1"
这个代表已经安装成功
4.测试是否在提交不规格时进行拦截
5.组员在提交代码规范的时候最好用 npx cz 规范来提交,不要用git commit