独书先生 Menu

Viewing all items for tag git commit

git commit规范

依赖安装

1. 生成commit信息

npm install --save-dev commitizen

npx commitizen init cz-conventional-changelog --save-dev --save-exact

package.json

"scripts": {
   "commit": "git-cz"
}

2. commit校验

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

echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js
修改文件commitlint.config.js为正常格式

3. 更新版本、生成changelog、打tag

npm i --save-dev standard-version

package.json

 "scripts": {
    "release": "standard-version"
  }

commit使用步骤

提交代码

git pull
git add .
npm run commit
npm run release -- --prerelease
git push --follow-tags origin master

commit规范

npm run commit后根据交互提示编写
1. type

type为必填项,用于指定commit的类型
# 主要type
feat:     增加新功能
fix:      修复bug

# 特殊type
docs:     只改动了文档相关的内容
style:    不影响代码含义的改动,例如去掉空格、改变缩进、增删分号
build:    构造工具的或者外部依赖的改动,例如webpack,npm
refactor: 代码重构时使用
revert:   执行git revert打印的message

# 不常使用type
test:     添加测试或者修改现有测试
perf:     提高性能的改动
ci:       与CI(持续集成服务)有关的改动
chore:    不修改src或者test的其余修改,例如构建过程或辅助工具的变动
复制代码当一次改动包括主要type与特殊type时,统一采用主要type。
  1. scope

scope也为必填项,用于描述改动的范围,例如在业务项目中可以依据菜单或者功能模块划分,如果是组件库开发,则可以依据组件划分

  1. subject

subject是commit的简短描述

  1. body

commit的详细描述,说明代码提交的详细说明。主要描述改动之前的情况及修改动机,对于小的修改不作要求,但是重大需求、更新等必须添加body来作说明。

  1. break changes

break changes指明是否产生了破坏性修改,涉及break changes的改动必须指明该项,类似版本升级、接口参数减少、接口删除、迁移等。

  1. affect issues

affect issues指明是否影响了某个问题。格式为: fix #{issue_id}
例如:

re #2
fix #14

发布版本

在多次commit之后,可能需要发布一个新版本,以下命令会自动更新版本号

npm run release -- --prerelease
git push --follow-tags origin master 

详细使用:https://github.com/conventional-changelog/standard-version

参考

  • https://juejin.im/post/5cea2c4bf265da1b6836993f
  • https://juejin.im/post/5cc4694a6fb9a03238106eb9
  • https://juejin.im/post/5d0b3f8c6fb9a07ec07fc5d0