前言:在使用git提交代码过程中,git commit的规范常不被大多数人注意,然而版本管理工具中,代码的提交规范对项目的后续维护有着很重要的作用。故本文从以下几个方面展开述说工程开发中git commit优化的几个工具:
- commitizen:简单的 commit 规范
- cz-conventional-changelog:执行会将项目npm发布新版本,并自动生成CHANGELOG.md文件
- commitlint:格式校验工具
- husky:Git的钩子,在此作用为代码的提交规范和规范的校验
- standard-version:辅助 cz-conventional-changelog 打 version 等功能
commitizen和cz-conventional-changelog
下载cz-conventional-changelog commitizen
package.json添加配置信息cnpm i -D commitizen cz-conventional-changelog
"scripts": {
...,
"commit": "git status && git add . && git-cz",
},
,
"config": {
"commitizen": {
"path": "node_modules/cz-conventional-changelog"
}
}
成功提交之后
Commitlint及husky
cnpm i -D husky @commitlint/config-conventional @commitlint/cli
项目根目录新建commitlint.config.js
module.exports = { extends: ['@commitlint/config-conventional']}
package.json添加如下
# package.json
...,
"husky": {
"hooks": {
"commit-msg": "commitlint -E $HUSKY_GIT_PARAMS" }
}
执行命令 npm run commit
1.Select the type of change that you're committing 选择改动类型 (<type>)
2.What is the scope of this change (e.g. component or file name)? 填写改动范围 (<scope>)
3.Write a short, imperative tense description of the change: 写一个精简的描述 (<subject>)
4.Provide a longer description of the change: (press enter to skip) 对于改动写一段长描述 (<body>)
5.Are there any breaking changes? (y/n) 是破坏性修改吗?默认n (<footer>)
6.Does this change affect any openreve issues? (y/n) 改动修复了哪个问题?默认n (<footer>)
生成如下格式
<type>(<scope>): <subject>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>
其中各自含义为
- scope 指 commit 的范围(哪些模块进行了修改)
- subject 指 commit 的简短描述
- body 指 commit 主体内容(长描述)
- footer 指 commit footer 信息
- type 指当前 commit 类型,一般有下面几种可选类型:
# 主要type
feat: 增加新功能
fix: 修复bug
# 特殊type
docs: 只改动了文档相关的内容
style: 不影响代码含义的改动,例如去掉空格、改变缩进、增删分号
build: 构造工具的或者外部依赖的改动,例如webpack,npm
refactor: 代码重构时使用
revert: 执行git revert打印的message
# 暂不使用type
test: 添加测试或者修改现有测试
perf: 提高性能的改动
ci: 与CI(持续集成服务)有关的改动
chore: 不修改src或者test的其余修改,例如构建过程或辅助工具的变动
standard-version: 自动生成 CHANGELOG
下载插件
cnpm i --save-dev standard-version
package.json添加如下
{
"scripts": {
"release": "standard-version"
}
}
执行npm run release,在根目录会生成CHANGELOG.md文件
总package.json
{
"name": "web-learn-notes",
"version": "1.1.0",
"description": "web学习笔记仓库",
"main": "index.js",
"scripts": {
"commit": "git status && git add . && git-cz",
"release": "standard-version",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/wztlink1013/web-learn-notes.git"
},
"keywords": [
"web",
"学习",
"笔记"
],
"author": "wztlink1013",
"license": "ISC",
"bugs": {
"url": "https://github.com/wztlink1013/web-learn-notes/issues"
},
"homepage": "https://github.com/wztlink1013/web-learn-notes#readme",
"devDependencies": {
"@commitlint/cli": "^16.0.0",
"@commitlint/config-conventional": "^16.0.0",
"commitizen": "^4.2.4",
"cz-conventional-changelog": "^3.3.0",
"husky": "^7.0.4",
"standard-version": "^9.3.2"
},
"config": {
"commitizen": {
"path": "node_modules/cz-conventional-changelog"
}
},
"husky": {
"hooks": {
"commit-msg": "commitlint -E $HUSKY_GIT_PARAMS"
}
}
}