git cz 规范化 git commit 格式

git cz 规范化 git commit 格式

  1. npm install git-cz --save-dev
  2. npm install commitizen --save-dev
  3. npm install cz-customizable --save-dev
javascript 复制代码
// 这是package.json自动生成的
"config": {
    "commitizen": {
      "path": "./node_modules/cz-customizable"
    },
  }
  1. 根目录新建 .cz-config.cjs
javascript 复制代码
// .cz-config.cjs
module.exports = {
  types: [
    { value: 'update',   name: 'update:   功能开发阶段性提交' },
    { value: 'feat',     name: 'feat:     完整新功能提交' },
    { value: 'fix',      name: 'fix:      修复Bug' },
    { value: 'perf',     name: 'perf:     性能优化(在不影响代码内部行为的前提下,对程序性能进行优化)' },
    { value: 'refactor', name: 'refactor: 代码重构(在不影响代码内部行为、功能下的代码修改)' },
    { value: 'docs',     name: 'docs:     只修改了文档相关的文件(e.g. 修改README.md)' },
    { value: 'style',    name: 'style:    代码风格、不影响代码功能的更改(e.g. 修改空格缩进,换行规范)' },
    { value: 'build',    name: 'build:    影响项目构建或依赖项修改(e.g. 升级webpack到版本5)' },
    { value: 'chore',    name: 'chore:    构建过程、辅助工具等相关的内容修改(e.g. 更新依赖库)' },
    { value: 'revert',   name: 'revert:   恢复上一次提交(e.g. 回滚feat: 增加用户注册功能)' },
  ],

  scopes: [],

  messages: {
    type: '选择一种你提交的类型(必选):',
    scope: '\n自定义更改范围(可选):',
    subject: '短说明(必填):\n',
    body: '长说明, 使用"|"换行(可选):\n',
    breaking: '列举非兼容性的重大变更(可选):\n',
    footer: '关联关闭的issue, 例如: #31, #34(可选)\n',
    confirmCommit: '确认提交?',
  },

  allowBreakingChanges: ['update', 'feat', 'fix'],
};
javascript 复制代码
// 如果 git cz 报错  Unable to find a configuration file,新增一条配置,指定配置文件地址
"config": {
    "commitizen": {
      "path": "./node_modules/cz-customizable"
    },

    // 手动新增
    "cz-customizable": {
      "config": ".cz-config.cjs"
    }
  }

避免绕过 git cz,直接用git commit - m '' 来提交,校验commit message

  1. npm install --save-dev husky
  2. npx husky-init

// 根目录自动生成了.husky文件夹

  1. npm i --save-dev @commitlint/config-conventional @commitlint/cli

  2. package.json 新增scripts

javascript 复制代码
"scripts": {
    "commitlint": "commitlint --config commitlint.config.cjs -e -V"
  },
  1. npx husky add .husky/commit-msg "npm run commitlint"

  2. 根目录新建 commitlint.config.cjs

javascript 复制代码
module.exports = {
  extends: ["@commitlint/config-conventional"],
  rules: {
    "type-enum": [0]
  }
};

现在直接git commit - m '随便填',会报错

项目的package.json(仅供参考)

javascript 复制代码
{
  "name": "integrated-platform",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "rimraf dist && vite build",
    "preview": "vite preview",
    "lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs --fix --ignore-path .gitignore",
    "format": "prettier --write src/",
    "commit": "git-cz",
    "commitlint": "commitlint --config commitlint.config.cjs -e -V",
    "prepare": "husky install"
  },
  "dependencies": {
    "@element-plus/icons-vue": "^2.3.1",
    "@vueuse/core": "^11.0.3",
    "axios": "^1.7.7",
    "crypto-js": "^4.2.0",
    "element-plus": "^2.8.1",
    "js-cookie": "^3.0.5",
    "pinia": "^2.1.7",
    "pre-commit": "^1.2.2",
    "vue": "^3.4.29",
    "vue-router": "^4.3.3"
  },
  "devDependencies": {
    "@commitlint/cli": "^19.4.1",
    "@commitlint/config-conventional": "^19.4.1",
    "@iconify-json/ep": "^1.2.0",
    "@iconify-json/ic": "^1.2.0",
    "@rushstack/eslint-patch": "^1.8.0",
    "@vitejs/plugin-vue": "^5.0.5",
    "@vitejs/plugin-vue-jsx": "^4.0.0",
    "@vue/eslint-config-prettier": "^9.0.0",
    "autoprefixer": "^10.4.20",
    "commitizen": "^4.3.0",
    "cz-conventional-changelog": "^3.3.0",
    "cz-customizable": "^7.2.1",
    "eslint": "^8.57.0",
    "eslint-plugin-vue": "^9.23.0",
    "git-cz": "^4.9.0",
    "husky": "^8.0.0",
    "less": "^4.2.0",
    "lint-staged": "^15.2.10",
    "postcss": "^8.4.45",
    "prettier": "^3.2.5",
    "rimraf": "^6.0.1",
    "tailwindcss": "^3.4.10",
    "unplugin-auto-import": "^0.18.2",
    "unplugin-icons": "^0.19.3",
    "unplugin-vue-components": "^0.27.4",
    "vite": "^5.3.1"
  },
  "lint-staged": {
    "**/*.+(js|jsx|ts|tsx|vue)": [
      "eslint --fix",
      "prettier --write"
    ]
  },
  "engines": {
    "node": "18.3.0"
  },
  "config": {
    "commitizen": {
      "path": "./node_modules/cz-customizable"
    },
    "cz-customizable": {
      "config": ".cz-config.cjs"
    }
  }
}

项目的目录结构(仅供参考)

mac 系统 husky 钩子不执行

bash 复制代码
git commit -m '1532'

hint: The '.husky/pre-commit' hook was ignored because it's not set as executable.
hint: You can disable this warning with `git config advice.ignoredHook false`.
hint: The '.husky/commit-msg' hook was ignored because it's not set as executable.
hint: You can disable this warning with `git config advice.ignoredHook false`.

[main af74cc4] 1532
 1 file changed, 1 insertion(+), 1 deletion(-)

husky 没有执行权限,项目终端输入,添加执行权限
chmod +x .husky/*

查看钩子的权限

bash 复制代码
 ls -l .husky/pre-commit

 // 只有读权限
 -rw-r--r--@ 1 user  admin  74 Sep  7 20:24 .husky/pre-commit

 chmod +x .husky/pre-commit

 // 加了执行权限
 -rwxr-xr-x 1 user group 1234 May 15 14:00 .husky/pre-commit
相关推荐
gqkmiss2 分钟前
Chrome 浏览器 131 版本开发者工具(DevTools)更新内容
前端·chrome·浏览器·chrome devtools
Summer不秃8 分钟前
Flutter之使用mqtt进行连接和信息传输的使用案例
前端·flutter
旭日猎鹰12 分钟前
Flutter踩坑记录(二)-- GestureDetector+Expanded点击无效果
前端·javascript·flutter
Viktor_Ye19 分钟前
高效集成易快报与金蝶应付单的方案
java·前端·数据库
hummhumm21 分钟前
第 25 章 - Golang 项目结构
java·开发语言·前端·后端·python·elasticsearch·golang
乐闻x1 小时前
Vue.js 性能优化指南:掌握 keep-alive 的使用技巧
前端·vue.js·性能优化
一条晒干的咸魚1 小时前
【Web前端】创建我的第一个 Web 表单
服务器·前端·javascript·json·对象·表单
Amd7941 小时前
Nuxt.js 应用中的 webpack:compiled 事件钩子
前端·webpack·开发·编译·nuxt.js·事件·钩子
生椰拿铁You1 小时前
09 —— Webpack搭建开发环境
前端·webpack·node.js
狸克先生1 小时前
如何用AI写小说(二):Gradio 超简单的网页前端交互
前端·人工智能·chatgpt·交互