【前端工程化】git commit 提交规范

  1. 全局安装 commitizen
sudo npm install commitizen -g
  1. 在项目目录下,添加 cz-conventional-changelog 适配器,直接使用 angular 的提交规范
commitizen init cz-conventional-changelog --save --save-exact

作用:

  1. package.json 中的 devDependencies 添加 cz-conventional-changelog 依赖,并通过 npm 安装依赖。
  2. package.json 中添加属性 config
  "config": {
    "commitizen": {
      "path": "./node_modules/cz-conventional-changelog"
    }
  }
  1. 自定义提交说明规范

    • 安装适配器
    npm install cz-customizable --save-dev
    
    • 修改第二步自动生成的配置(package.json中)
    {
      "config":{
        "commitizen": {
            "path": "node_modules/cz-customizable"
        }
    	}
    }
    
    • 根目录下新建自定义适配器配置文件,.cz-config.js
    module.exports = {
      types: [
        {
          value: "feat",
          name: "feat:      一个新的特性"
        },
        {
          value: "fix",
          name: "fix:       修复一个bug"
        },
        {
          value: "docs",
          name: "docs:      仅更新了文档"
        },
        {
          value: "style",
          name: "style:     代码格式调整"
        },
        {
          value: "refactor",
          name: "refactor:  重构,既不修复错误也不添加功能的代码更改"
        },
        {
          value: "perf",
          name: "perf:      提高性能的代码更改"
        },
        {
          value: "test",
          name: "test:      添加测试代码"
        },
        {
          value: "chore",
          name: "chore:     构建工具,脚手架等更改"
        },
        { value: "revert", name: "revert:    代码回退" },
        { value: "WIP", name: "WIP:       正在进行的工作" }
      ],
      scopes: [
        {
          name: "模块1"
        }
      ],
      messages: {
        type: "选择一种你的提交类型",
        scope: "选择一个scope(可选)",
        subject: "短说明:\n",
        body: '长说明,使用"|"换行(可选):\n',
        breaking: "非兼容性说明(可选):\n",
        footer: "关联关闭的issue,例如:#1234(可选):\n",
        confirmCommit: "确定提交说明?"
      },
      allowCustomScopes: true,
      allowBreakingChanges: ["feat", "fix"],
      subjectLimit: 100
    };
    
  2. 添加 commit 校验

    npm install --save-dev @commitlint/cli
    
    npm install --save-dev @commitlint/config-conventional 
    

    新建 commitlint.config.js配置校验规则

    module.exports = {
      extends: ['@commitlint/config-conventional']
    };
    

    安装 huksy => git 钩子工具

    npm install husky --save-dev
    

    package.json 中配置 git commit 提交时的校验钩子

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

    我在测试提交的时候,如果是先安装了依赖,再初始化 git 仓库时,提交校验失效。需要先初始化 git 仓库,然后再安装依赖。

  3. git 提交说明结构

结构块HeaderBodyFooter
包含内容type(必须),scope(可选),subject(必须)Commit 的详细描述不兼容变更和关闭缺陷说明
格式():

各字段说明:

字段名解释/可选值
typefeat - 新功能
scope模块名
subject提交的简短描述
body提交的详细描述
footer关闭的ISSUE

提交后的结果类似如下

docs(模块1): 更新文档

仅更新了README.md的文档说明

ISSUES CLOSED: #1
  1. 代码提交
git cz 
  1. Changelog 生成

安装依赖

npm install conventional-changelog-cli --save-dev

修改 package.json 配置

"scripts": {
    ...,
    "version": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0 && git add CHANGELOG.md"
}

生成 changelog

npm run version

生成 chagnelog 只能生成当前版本的。

posted @   haokur  阅读(86)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示
主题色彩