项目初始化 - git commit规范与配置

737 阅读1分钟

Commitizen:是一个命令行提示工具,它主要用于帮助我们更快地写出规范的commit message Husky:是一个git hook工具,用于在提交过程中的某个特定时刻触发commitlint

1、需求场景

在一个开发团队中,前端开发的代码管理越来越规范化、工程化,而代码commit更是需要一个统一的规范去约束,保证代码commit时的规范性。尤其多人参与一个项目开发时,大家的代码commit风格不相同,不便于后续的代码统一管理和可读性;所以良好的git commit风格是很重要的。

2、使用commit工具约束commitizen

git是现在市面上最流行的版本控制工具

  • 安装
npm install commitizen -g
npm i commitizen -D
commitizen init cz-conventional-changelog --save-dev --save-exact
  • 在package.json添加
  "scripts": {
    "commit": "cz"
  }
  • 使用
npm run commit 
// 或者使用
git cz

image.png

image.png

常见commit提交type

# 主要type
feat:     增加新功能
fix:      修复bug
 
# 特殊type
docs:     只改动了文档相关的内容
style:    不影响代码含义的改动,例如去掉空格、改变缩进、增删分号
build:    构造工具的或者外部依赖的改动,例如webpack,npm
refactor: 代码重构时使用
revert:   执行git revert打印的message
 
# 暂不使用type
test:     添加测试或者修改现有测试
perf:     提高性能的改动
ci:       与CI(持续集成服务)有关的改动
chore:    不修改src或者test的其余修改,例如构建过程或辅助工具的变动

3、Husky

npm install husky --save-dev
npm set-script prepare "husky install"
npm run prepare
# npx husky add .husky/pre-commit "npm test"
git add .husky/pre-commit

参考链接

commitizen npm husky npm