规范Git commit
规范commit的好处
- 便于程序员对提交历史进行追溯
- 一旦约束了commit message,意味着我们将慎重的进行每一次提交,不能再一股脑的把各种各样的改动都放在一个commit里面。使得代码改动的历史更加清晰。
- 格式化的commit message才可以用于自动化输出Change log。
commit的类别:
build
|
Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
|
对构建,依赖,架构,框架上的改动
|
---|---|---|
ci | Changes to our CI configuration files and scripts (example scopes: Circle, BrowserStack, SauceLabs) | 对CI配置的改动 |
docs | Documentation only changes | 对文档的改动 |
feat | A new feature | 对功能的改动 |
fix | A bug fix | 对Bug的改动 |
perf | A code change that improves performance | 对性能优化的改动 |
refactor | A code change that neither fixes a bug nor adds a feature | 对代码进行重构 |
revert | Revert codes | 代码回滚 |
test | Adding missing tests or correcting existing tests | 对测试用例的改动 |
Husky
需要加上husky和commitlint的搭配检查这些commit。
husky是一个npm包,意味着仅仅在使用命令行提交代码的同时,会触发对应的hook脚本。
安装
安装husky,commitlint以及它的angular规范:
yarn add husky @commitlint/config-angular @commitlint/cli -D
配置
在package.json 中进行配置:
"husky": {
"hooks": {
"pre-commit": "yarn lint",
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
}
以上配置了两个钩子事件。
pre-commit:顾名思义,commit前触发的事件
commit-msg:检测commit的msg是否符合规范,即commitlint.config.js里面指定的规范
在根目录创建文件:commitlint.config.js 并写入以下内容:
module.exports = { extends: ['@commitlint/config-angular'] };
Git操作
在提交代码时,建议在vscode里面进行操作。
1. 选取需要commit的文件:
vscode自带的git控制面板,可以很方便地勾选需要commit的文件。如果全部都需要提交,可以用命令行代替:
git add -u : 仅监控已经被add的文件,不包括新添加的文件(git add --update的缩写)
git add . : 文件内容修改(modified)以及新文件(new),但不包括被删除的文件。
git add -A : 是上面两个功能的合集(git add --all的缩写)
2. 提交
提交语句一定要通过命令行运行。
git commit -m "fix: 解决了查询时页面不刷新的问题"
3. 同步
git pull
git push