持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第22天,点击查看活动详情
前情提要
在前端日常开发过程中离不开代码的版本管理git、svn。几乎是相生相随,而对于git的使用程度和频率也是极其之高。基本上工作日每天都会提交代码很多次,休息日的时候自己写demo,写开源项目也会提交很多次代码。但是每次我们提交代码都是:git commit -m"xxxx"。用的久了就会显得少了很多东西,我们可以通过一些插件规范化提交信息。
准备工作
我们需要在项目里安装四个插件:
- @commitlint/config-conventional
- 注意安装该插件的时候记得用双引号包起来,否则npm i的时候会报错,无法install
 
- commitizen
- commitlint
- cz-customizable
- 在项目的根目录创建两个文件:
- .commitlintrc.js
- .cz-config.js
 
开始配置
- .commitlintrc.js
module.exports = {
    extends: [ "@commitlint/config-conventional" ]
}
- .cz-config.js
module.exports = {
    types: [
        {
            value: 'feat',
            name: 'feat: 新功能'
        },
        {
            value: 'fix',
            name: 'fix: 修复bug'
        },
        {
            value: 'init',
            name: 'init: 初始化'
        },
        {
            value: ':pencid2: docs',
            name: 'docs: 文档变更'
        },
        {
            value: 'style',
            name: 'style: 代码的样式美化'
        },
        {
            value: 'refactor',
            name: 'refactor: 重构'
        },
        {
            value: 'perf',
            name: 'perf: 性能优化'
        },
        {
            value: 'test',
            name: 'test: 测试'
        },
        {
            value: 'revert',
            name: 'revert: 回退'
        },
        {
            value: 'build',
            name: 'build: 打包'
        },
        {
            value: 'chore',
            name: 'chore: 构建/工程依赖/工具'
        },
        {
            value: 'ci',
            name: 'ci: CI related changes'
        },
    ],
    messages: {
        type: '请选择提交类型(必填)',
        customScope: '请输入文件修改范围(可选)',
        subject: '请简要魔术提交(必填)',
        body: '请输入详细描述(可选)',
        breaking: '列出任何 BREAKING CHANGES(可选)',
        footer: '请输入要关闭的issue(可选)',
        confirmcOMMIT: '确定提交此说明?'
    },
    allowCustomScopes: true,
    allowBreakingChanges: [ 'feat', 'fix' ], // 当提交类型为feat,fix时才有破坏性修改选项
    subjectLimit: 72
}
- 最后在package.json文件中增加启动项:
“scripts”: {
    "commit": "git add . & cz"
}
- 至此多人协作开发中的git规范配置就搞定了。
- 最后我们在开发结束之后执行:npm run commit命令,然后按照控制台的提示填写信息就行了。
总结
- 多人协作开发是永远磨不开的话题,遵守统一的规范是重中之重,也是高效开发的必备条件之一。
- 那么对于前端开发中团队中使用的git提交规范还有哪些?