优雅的提交commit
背景
个人角度
自己的github上的一个库,由于在yarn的时候更新了husky这个依赖包,但是并没有去进行git提交日志的规范配置,导致提交时commit信息被拦截,然后稍微排查了一下,发现自己的日志信息时真的乱,哈哈哈哈,然后对git commit 规范化配置进行了一波学习。
小组开发角度
之前在项目小组开发过程中,是有对commit的简单限制,例如:

但仍会出现commit提交信息重复,无法对具体更新进行详细描述,没有具体配置等,所以团队的协作规范化蛮重要的。
- 规范化
 - 一体化
 - 简约化
 
沉迷在团队协作的魅力中无法自拔
然而事实真的是这样的,在多人协作中我们不得不面临,
- 代码合并冲突,
 - 切换错误分支开发导致代码混乱,
 - 想要cherry-pick时找不到想pick的分支等等
 
需要工具
commitizen  
cz-conventional-changelog 
husky
实操过程
第一步
我们可以尝试在全局安装一个包 commitizen
npm install -g commitizen
第二步
- 首先全局安装 
cz-conventional-changelog 
npm install -g cz-conventional-changelog
- 然后在项目里安装
 
commitizen init cz-conventional-changelog --save --save-exact
接着你可以看下你项目的package.json ,会多出一部分配置
"devDependencies": {
    "cz-conventional-changelog": "^3.3.0",
  },
  "config": {
    "commitizen": {
      "path": "node_modules/cz-customizable"
    }
  }
如果安装完成以后没有config的话自己cv一下
在根目录下创建文件.cz-config
  module.exports = {
  // 可选类型
  types: [
    { value: 'feat', name: 'feat:     新功能' },
    { value: 'fix', name: 'fix:      修复' },
    { value: 'docs', name: 'docs:     文档变更' },
    { value: 'style', name: 'style:    代码格式(不影响代码运行的变动)' },
    {
      value: 'refactor',
      name: 'refactor: 重构(既不是增加feature,也不是修复bug)',
    },
    { value: 'perf', name: 'perf:     性能优化' },
    { value: 'test', name: 'test:     增加测试' },
    { value: 'chore', name: 'chore:    构建过程或辅助工具的变动' },
    { value: 'revert', name: 'revert:   回退' },
    { value: 'build', name: 'build:    打包' },
  ],
  // 消息步骤
  messages: {
    type: '请选择提交类型:',
    customScope: '请输入修改范围(可选):',
    subject: '请简要描述提交(必填-最长72字节):',
    body: '请输入详细描述(可选):',
    footer: '请输入要关闭的issue(可选):',
    confirmCommit: '确认使用以上信息提交?(y/n/e/h)',
  },
  // 跳过问题
  // skipQuestions: ['body', 'footer'],
  // subject文字长度默认是72
  subjectLimit: 72,
};
之后需要 git commit  的操作全部换成 git cz
第三步
项目内安装 commitlint
yarn add @commitlint/config-conventional @commitlint/cli
之后你的 package.json 又会多出一部分的配置
  "dependencies": {
    "@commitlint/cli": "^17.6.3",
    "@commitlint/config-conventional": "^17.6.3"
  }
接着在package.json 统计目录新建 commitlint.config.js 文件 然后写入
module.exports = {
  extends: ['@commitlint/config-conventional'],
  rules: {
    'type-enum': [
      // type枚举
      2,
      'always',
      [
        'build', // 编译相关的修改,例如发布版本、对项目构建或者依赖的改动
        'feat', // 新功能
        'fix', // 修补bug
        'docs', // 文档修改
        'style', // 代码格式修改, 注意不是 css 修改
        'refactor', // 重构
        'perf', // 优化相关,比如提升性能、体验
        'test', // 测试用例修改
        'revert', // 代码回滚
        'ci', // 持续集成修改
        'config', // 配置修改
        'chore', // 其他改动
      ],
    ],
    'type-empty': [2, 'never'], // never: type不能为空; always: type必须为空
    'type-case': [0, 'always', 'lower-case'], // type必须小写,upper-case大写,camel-case小驼峰,kebab-case短横线,pascal-case大驼峰,等等
    'scope-empty': [0],
    'scope-case': [0],
    'subject-empty': [2, 'never'], // subject不能为空
    'subject-case': [0],
    'subject-full-stop': [0, 'never', '.'], // subject以.为结束标记
    // 'header-max-length': [2, 'always', 72], // header最长72
    // 'body-leading-blank': [0], // body换行
    // 'footer-leading-blank': [0, always], // footer以空行开头
  },
};
第四步
项目中安装husky
yarn add husky
接着配置 husky
"dependencies": {
    "@commitlint/cli": "^8.3.5",
    "@commitlint/config-conventional": "^8.3.4",
    "husky": "^4.2.5"
  },
  "husky": {
    "hooks": {
      "commit-msg": "commitlint -e $GIT_PARAMS"
    }
  }
当我们去以不合法的提交信息进行提交代码时,会进行检查
总结
整个提交的流程,大概就是这样的:


补充
转载自:https://juejin.cn/post/7237702874880933946