git commit 规范及自动化

文章目录

  • 规范描述
      • [header 格式](#header 格式)
        • [type 必填](#type 必填)
        • [scope 选填](#scope 选填)
        • subject
  • 项目配置
      • [1. 安装](#1. 安装)
      • [2. 配置文件](#2. 配置文件)
      • [3. 自动生成 Change Log](#3. 自动生成 Change Log)

规范描述

commit message需要包括两部分内容:header和body

  • header(推荐):简要描述此次commit的改动范围/内容
  • body(可选):若代码出现较大改变时填写

header 格式

header部分只有一行,包括三个字段:

复制代码
<type>(<scope>):<subject>
type 必填

说明commit类型,只允许使用以下标识

  • breaking:不兼容的改动,接口删除、数据库字段更新等,具体不兼容的部分用scope说明
  • feat:新功能(feature)
  • fix:修复bug
  • perf:优化(包括提升性能、体验)
  • refactor:重构(不是新增功能,也不是修改bug的代码改动)
  • docs:文档调整(documentation)
  • style:格式调整
  • test:测试调整(增加测试用例等)
  • chore:构建过程或辅助工具的变动
  • revert:回滚到某个版本
scope 选填

说明commit更改的文件名,多个用","分开

subject

commit简短描述

项目配置

1. 安装

python 复制代码
// 全局安装
npm install commitizen -g
// 项目目录下安装
npm i commitlint --save-dev
npm i @commitlint/config-conventional --save-dev
npm i husky --save-dev
npm install commitizen --save-dev
# 用于自动生成 change log
npm i conventional-changelog-cli --save-dev

2. 配置文件

在项目目录下,新建配置文件 commitlint.config.js

python 复制代码
module.exports = {
  extends: ['@commitlint/config-conventional'],
  rules: {
    // type 类型定义
    'type-enum': [2, 'always', [
      "feat", // 新功能 feature
      "fix", // 修复 bug
      "docs", // 文档注释
      "style", // 代码格式(不影响代码运行的变动)
      "refactor", // 重构(既不增加新功能,也不是修复bug)
      "perf", // 性能优化
      "test", // 增加测试
      "chore", // 构建过程或辅助工具的变动
      "revert", // 回退
      "build" // 打包
    ]],
    // subject 大小写不做校验
    // 自动部署的BUILD ROBOT的commit信息大写,以作区别
    'subject-case': [0]
  }
};

在项目目录下,新建配置文件 .cz-config.js

python 复制代码
'use strict';

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:    打包'}
  ],
  // override the messages, defaults are as follows
  messages: {
    type: '请选择提交类型:',
    // scope: '请输入文件修改范围(可选):',
    // used if allowCustomScopes is true
    customScope: '请输入修改范围(可选):',
    subject: '请简要描述提交(必填):',
    body: '请输入详细描述(可选,待优化去除,跳过即可):',
    // breaking: 'List any BREAKING CHANGES (optional):\n',
    footer: '请输入要关闭的issue(待优化去除,跳过即可):',
    confirmCommit: '确认使用以上信息提交?(y/n/e/h)'
  },
  allowCustomScopes: true,
  // allowBreakingChanges: ['feat', 'fix'],
  skipQuestions: ['body', 'footer'],
  // limit subject length, commitlint默认是72
  subjectLimit: 72
};

在package.json文件中增加相关配置

python 复制代码
{
  ...
  "scripts": {
    ...,
    "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
  },
  ...,
  "husky": {
    "hooks": {
      "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
    }
  },
  "config": {
    "commitizen": {
      "path": "./node_modules/cz-customizable"
    }
  }
}

3. 自动生成 Change Log

运行 npm run changelog

虽然只能生成简短的 commit 提交记录,但是已经提供了框架和基本 log

手动修改生成后的 log 文件即为项目 log

相关推荐
阿巴阿巴boer几秒前
用wsl搭建远程linux服务器
linux·运维·服务器·ssh
lisanmengmeng40 分钟前
docker 方式安装部署禅道zentao(五)
运维·docker·容器
Logic1011 小时前
《告别重复劳动!易语言实现档案数字化录入全自动助手》
自动化·实战项目·档案管理·易语言·办公效率·模拟按键·文字录入
乐之者v1 小时前
使用Git 要注意的
git
wanhengidc1 小时前
云手机的硬件依赖性如何?
运维·服务器·智能手机·云计算
piaoroumi2 小时前
UVC调试
linux·运维·前端
刘洋浪子2 小时前
Git命令学习
git·学习·elasticsearch
VekiSon2 小时前
Linux系统编程——标准IO
linux·运维·服务器
Evan芙2 小时前
DNS服务器类型,解析答案,正反解析域,资源记录定义总结
运维·服务器
wanhengidc2 小时前
巨椰 云手机办公便利性高
运维·服务器·安全·智能手机·云计算