• Git Commit规范指北


    在这里插入图片描述

    参考文章

    背景

    Git 每次提交代码,都要写 Commit message(提交说明),提交内容也五花八门,不便于后续的项目阅读和管理。

    优点

    • 可读性好,清晰,不必深入看代码即可了解当前commit的作用。
    • 为 Code Reviewing做准备
    • 方便跟踪工程历史
    • 让其他的开发者在运行 git blame 的时候想跪谢
    • 方便生成修改日志(CHANGELOG.MD)

    git blame => 查看文件的每一行是谁修改的

    如下:查看 index.html中10行2列修改人

    git blame -L 10,+2 index.html
    
    • 1

    规范

    可以使用典型的Git工作流程或通过使用CLI向导Commitizen来添加提交消息格式。

    使用界面:

    在这里插入图片描述

    安装

    推荐全局安装,项目中配置即可

    # 全局安装
    pnpm install -g commitizen cz-conventional-changelog
    
    • 1
    • 2

    配置

    在package文件中,添加以下代码:

    "config": {
        "commitizen": {
            "path": "cz-conventional-changelog"
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    使用

    凡是用到git commit命令,一律改为使用git cz。

    此时会出现这样的交互式命令行,根据提示完成提交

    ? Select the type of change that you're committing: (Use arrow keys)
    ❯ feat:        A new feature 
      fix:         A bug fix 
      improvement: An improvement to a current feature 
      docs:        Documentation only changes 
      style:       Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc) 
      refactor:    A code change that neither fixes a bug nor adds a feature 
      perf:        A code change that improves performance 
    (Move up and down to reveal more choices)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    使用

    凡是用到git commit命令,一律改为使用git cz。这时,就会出现选项,用来生成符合格式的 Commit message。

    编辑器插件

    如果你使用的是webstorm,在plugin中搜索commit安装即可

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Onis3PIP-1662129877075)(?)]

    到此就可以使用 git cz 来替换git commit了,
    最后给你项目的README加上一个标识吧。

    [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
    
    • 1

    格式

    每次提交,Commit message 都包括三个部分:header,body 和 footer。

    (): 
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Header

    Header部分只有一行,包括三个字段:type(必需)、scope(可选)和subject(必需)。

    • 1.type

    用于说明 commit 的类别,只允许使用下面7个标识。

    commit类别 - 说明

        feat:    |  新功能(feature)
        fix:     |  修补bug
        docs:    |  文档(documentation)
        style:   |  格式(不影响代码运行的变动)
        refactor:|  重构(即不是新增功能,也不是修改bug的代码变动)
        test:    |  增加测试
        chore:   |  构建过程或辅助工具的变动    
        revert:  |  commit 回退
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 2.scope

    scope用于说明 commit 影响的范围,比如数据层、控制层、视图层等等,视项目不同而不同。

    • 3.subject

    subjectcommit 目的的简短描述,不超过50个字符。

    Body

    Body 部分是对本次 commit 的详细描述,可以分成多行。下面是一个范例。

    Footer

    Footer 部分只用于以下两种情况:

    不兼容变动

    关闭 Issue

    commitlint 规范校验

    安装

    husky

    • 安装husky

    通过特定的 git hook 来检查git提交信息,代码lint等功能

    npm install --save-dev husky
    
    
    • 1
    • 2
    • 安装commitlint
    pnpm install -D @commitlint/cli @commitlint/config-conventional
    
    
    • 1
    • 2

    添加配置

    package.json添加如下配置

    "commitlint": {
       "extends": [
            "@commitlint/config-conventional"
       ]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    使用 husky 命令在,创建 commit-msg 钩子,并执行验证命令:

    npx husky add .husky/commit-msg "npx --no-install commitlint --edit $1"
    
    • 1

    添加标识

    [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
    
    • 1

    无权问题

    在这里插入图片描述

    chmod ug+x .husky/*
    chmod ug+x .git/hooks/*
    
    • 1
    • 2

    自动版本管理和生成CHANGELOG

    版本管理和CHANGELOG

    完整的package.json

    commitizen + cz-conventional-changelog + commitlint +standard-version

    {
        "name": "blog",
        "version": "1.0.0",
        "description": "blog",
        "main": "index.js",
        "scripts": {
            "start": "npm run dev",
            "release": "standard-version",
            "release:alpha": "standard-version --prerelease alpha",
            "release:rc": "standard-version --prerelease rc",
            "release:major": "npm run release -- --release-as major",
            "release:minor": "npm run release -- --release-as minor",
            "release:patch": "npm run release -- --release-as patch"
        },
        "devDependencies": {
            "@commitlint/cli": "^17.6.6",
            "@commitlint/config-conventional": "^17.6.6",
            "commitizen": "^4.2.2",
            "cz-conventional-changelog": "^3.3.0",
            "standard-version": "^9.1.0"
        },
        "config": {
            "commitizen": {
                "path": "cz-conventional-changelog"
            }
        },
        "commitlint": {
            "extends": [
                "@commitlint/config-conventional"
            ]
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
  • 相关阅读:
    使用flex布局实现,7种经典布局案例
    java-php-python-ssm绿色农产品推广应用网站计算机毕业设计
    windows上使用Gitblit搭建git服务仓库
    【Mysql】第4篇--多表查询和事务
    四、PL/SQL程序控制语句
    GB28181学习(十七)——基于jrtplib实现tcp被动和主动发流
    Spring Message源码解析
    ESP8266使用记录(一)
    Golang vs Rust ——服务端编程应该选择哪种语言
    【白板推导系列笔记】线性分类-线性判别分析(Fisher)-模型定义
  • 原文地址:https://blog.csdn.net/WEIGUO19951107/article/details/126671617