Git Commit规范指北

发布于:2022-12-31 ⋅ 阅读:(328) ⋅ 点赞:(0)

在这里插入图片描述

参考文章

背景

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

优点

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

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

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

git blame -L 10,+2 index.html

规范

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

使用界面:

在这里插入图片描述

安装

# 全局安装
npm install -g commitizen 
# 或者本地安装
npm install --save-dev commitizen
# 安装适配器
npm install cz-conventional-changelog

配置

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

    "config": {
        "commitizen": {
            "path": "cz-conventional-changelog"
        }
    }

使用

凡是用到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/)

格式

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

<type>(<scope>): <subject>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>

Header

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

  • 1.type

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

commit类别 - 说明

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

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

  • 3.subject

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

Body

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

Footer

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

不兼容变动

关闭 Issue

格式校验commitlint

安装依赖

npm install --save-dev @commitlint/{config-conventional,cli}
# 安装husky
npm install --save-dev husky

添加配置

添加 commitlint.config.js文件到项目

echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js

package.json配置

"husky": {
    "hooks": {
      "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
    }
  },

添加标识

在这里插入图片描述

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

无权问题

在这里插入图片描述

chmod ug+x .husky/*
chmod ug+x .git/hooks/*

自动版本管理和生成CHANGELOG

版本管理和CHANGELOG

完整的package.json

commitizen + cz-conventional-changelog + 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": {
        "commitizen": "^4.2.2",
        "cz-conventional-changelog": "^3.3.0",
        "standard-version": "^9.1.0"
    },
    "config": {
        "commitizen": {
            "path": "cz-conventional-changelog"
        }
    }
}
本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到