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

相关推荐
热爱嵌入式的小许5 小时前
Linux基础项目开发1:量产工具——显示系统
linux·运维·服务器·韦东山量产工具
Pythonliu710 小时前
茴香豆 + Qwen-7B-Chat-Int8
linux·运维·服务器
你疯了抱抱我10 小时前
【RockyLinux 9.4】安装 NVIDIA 驱动,改变分辨率,避坑版本。(CentOS 系列也能用)
linux·运维·centos
小O_好好学11 小时前
CentOS 7文件系统
linux·运维·centos
哲伦贼稳妥12 小时前
一天认识一个硬件之机房地板
运维·网络·经验分享·其他
john_hjy12 小时前
11. 异步编程
运维·服务器·javascript
x晕x12 小时前
Linux dlsym符号查找疑惑分析
linux·运维·服务器
活跃的煤矿打工人12 小时前
【星海saul随笔】Ubuntu基础知识
linux·运维·ubuntu
tangdou36909865513 小时前
两种方案手把手教你多种服务器使用tinyproxy搭建http代理
运维·后端·自动化运维
北京智和信通13 小时前
云平台和虚拟化智慧运维监控,全面提升故障感知与处置能力
运维·虚拟化·云平台·虚拟机监控