惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
WordPress大学
WordPress大学
小众软件
小众软件
云风的 BLOG
云风的 BLOG
IT之家
IT之家
人人都是产品经理
人人都是产品经理
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
T
Tailwind CSS Blog
阮一峰的网络日志
阮一峰的网络日志
V
V2EX
宝玉的分享
宝玉的分享
博客园 - Franky
F
Fortinet All Blogs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
GbyAI
GbyAI
Hugging Face - Blog
Hugging Face - Blog
Jina AI
Jina AI
D
Docker
博客园 - 聂微东
C
Check Point Blog
H
Help Net Security

OhYee 博客

小鹏辅助驾驶测评|OhYee 博客 小鹏非支持手机开启自动解锁|OhYee 博客 使用函数计算实现 301 重定向|OhYee 博客 针对 HTML 内容使用 Ant Design 图片弹框|OhYee 博客 博客进程泄露及僵尸进程解决|OhYee 博客 蓝易云服务器体验|OhYee 博客 SSH 调起本地 VSCode|OhYee 博客 【2022 秋招内推】阿里云后端研发工程师|OhYee 博客 使用函数计算获取 IP 地址信息|OhYee 博客 正确获取客户端 IP/HTTP Header 也可能重复|OhYee 博客 评测 Oculus Quest2 及 BigScreen|OhYee 博客 NextJS 热重载保留状态|OhYee 博客 如何优雅地贴 gist 代码|OhYee 博客 Linux 精细化文件权限|OhYee 博客 VSCode 容器开发环境|OhYee 博客 Clash 的不兼容更新排查|OhYee 博客 Zeek 导出 PCAP|OhYee 博客 记一次 ssh 配置问题|OhYee 博客 谈谈《星之卡比-探索发现》|OhYee 博客 VSCode 快捷键绑定 Shell 命令|OhYee 博客 ASN.1 语法及 X.509 证书格式解析解析|OhYee 博客 腾讯企业邮箱忽略 MX 记录发信|OhYee 博客 Chrome/Edge 标签组插件|OhYee 博客 【应届内推】阿里云后端研发工程师|OhYee 博客 损坏的 Typecho 备份处理为 JSON|OhYee 博客 VS Code VIM 插件高效使用|OhYee 博客 SSH 正反向代理|OhYee 博客 Let's Encrypt 根证书过期引发的问题|OhYee 博客 OpenWRT 忽略内核依赖|OhYee 博客 V2Ray + TLS + WS(WSS) Docker 部署|OhYee 博客
Git Commit 规范化工具|OhYee 博客
2022-04-12 · via OhYee 博客

Git Commit 规范化工具

导致 git commit 规范性较差的本质原因是英语不好

格式规范

对于比较通用的规范而言,commit 信息为如下格式

<type>[(<scope>)]: (<emoji>) <subject>

[body]

[breaking changes]

[footer]

一个完整的 commit 可能长如下的模样

fix(api): 🐛 params type check for GetUser

- check userid is number
- check username is string

BREAKING CHANGE :
can not use in v1.2.3

closed #1, #2, #3

对于一个 commit,其至少应该包含 typesubject 两部分。

  • type 可能包含以下几种可能:
    • chore: 构建过程或辅助工具更改
    • ci: 'CI 相关更改
    • docs: 文档更改
    • feat: 新功能特性
    • fix: 修复 Bug
    • perf: 性能优化
    • refactor: 功能重构(未修复 Bug 或添加功能)
    • release: 发布新版本
    • style: 修改代码样式(缩进、空格、换行、分号)
    • test: 添加测试样例
  • subject 是对于改动的简短描述,用一句话进行概述
  • scope 是改动涉及的范围,如 xx 模块、xx 分层。如果不需要可以省略
  • emojitype 绑定,是固定的 emoji 表情
  • body 是对于改动详细描述,可以使用多行描述
  • breaking changes 用于描述与旧版本不兼容的改动,也可以用来生成更新日志
  • footer 用于描述与 Github 等系统的联动,如关联 issue、PR

工具

大部分情况下,手打 commit 并不麻烦。但是对于不同的项目,scope 等信息可能是固定的,通过选择更方便填写(同时避免错误)

因此适当的第三方工具可以有效辅助 commit 填写

比较了相关的项目,推荐使用 commitizen/cz-cliZhengqbbb/cz-git 组合使用(其他工具在涉及换行等需要时,可能存在问题)

cz-gitcz-git

安装依赖于 npm

npm install -g cz-git commitizen

安装完成后还需要在用户目录添加配置文件 ~/.czrc
如果对于不同的项目,需要添加不同的配置,也可以在项目目录中进行配置(详见官方文档)

{
    "path": "cz-git",
    "messages": {
        "type": "选择你的提交类型   | Select the type of change that you're committing:",
        "scope": "选择一个模块范围(可选) | Denote the SCOPE of this change (optional)",
        "customScope": "自定义修改模块名 | Denote the SCOPE of this change:",
        "subject": "简短说明 | Write a SHORT, IMPERATIVE tense description of the change:\n",
        "body": "详细说明(可选) 使用\"|\"可换行 \n  Provide a LONGER description of the change (optional). Use \"|\" to break new line:\n",
        "breaking": "非兼容性说明(可选) 使用\"|\"可换行 | List any BREAKING CHANGES (optional):\n",
        "footerPrefixsSelect": "选择关联issue前缀 | Select the ISSUES type of changeList by this change (optional):",
        "customFooterPrefixs": "输入自定义issue前缀 | Input ISSUES Prefix:",
        "footer": "列举关联issue (可选) 例如: #31, #I3244  List any ISSUES CLOSED by this change (optional) :\n",
        "confirmCommit": "是否提交或修改commit | Are you sure you want to proceed with the commit above?"
    },
    "types": [
        {
            "value": "feat",
            "name": "feat:     新增功能 | A new feature"
        },
        {
            "value": "fix",
            "name": "fix:      修复缺陷 | A bug fix"
        },
        {
            "value": "release",
            "name": "release:  发布版本 | release a new version"
        },
        {
            "value": "docs",
            "name": "docs:     文档更新 | Documentation only changes"
        },
        {
            "value": "style",
            "name": "style:    代码格式 | Changes that do not affect the meaning of the code"
        },
        {
            "value": "refactor",
            "name": "refactor: 代码重构 | A code change that neither fixes a bug nor adds a feature"
        },
        {
            "value": "perf",
            "name": "perf:     性能提升 | A code change that improves performance"
        },
        {
            "value": "test",
            "name": "test:     测试相关 | Adding missing tests or correcting existing tests"
        },
        {
            "value": "build",
            "name": "build:    构建相关 | Changes that affect the build system or external dependencies"
        },
        {
            "value": "ci",
            "name": "ci:       持续集成 | Changes to our CI configuration files and scripts"
        },
        {
            "value": "revert",
            "name": "revert:   回退代码 | Revert to a commit"
        },
        {
            "value": "chore",
            "name": "chore:    其他修改 | Other changes that do not modify src or test files"
        }
    ],
    "useEmoji": false,
    "scopes": [],
    "allowCustomScopes": true,
    "allowEmptyScopes": true,
    "customScopesAlign": "bottom",
    "customScopesAlias": "custom",
    "emptyScopesAlias": "empty",
    "upperCaseSubject": false,
    "allowBreakingChanges": [
        "feat",
        "fix"
    ],
    "breaklineNumber": 100,
    "breaklineChar": "|",
    "skipQuestions": [],
    "issuePrefixs": [
        {
            "value": "link",
            "name": "link:     将任务状态更改为进行中"
        },
        {
            "value": "closed",
            "name": "closed:   ISSUES 已经解决"
        }
    ],
    "customIssuePrefixsAlign": "top",
    "emptyIssuePrefixsAlias": "skip",
    "customIssuePrefixsAlias": "custom",
    "confirmColorize": true,
    "maxHeaderLength": null,
    "maxSubjectLength": null,
    "minSubjectLength": 0,
    "defaultBody": "",
    "defaultIssues": "",
    "defaultScope": "",
    "defaultSubject": ""
}

而后,使用 git cz 替代 git commit 即可

参考资料