git 约定提交

时间:2021-09-05
本文章向大家介绍git 约定提交,主要包括git 约定提交使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

约定式提交

是 git 提交消息的约定,最有名的规范大概是 Angular 约定

结构

  • <> : 必填
  • [] : 选填
<type>[scope]: <subject>

[body]

[footer]

// 翻译
<类型>[范围]: <描述>

[正文]

[脚注]

type(提交类型)

  • feature:新功能
  • fix:修复bug
  • refactor:保证原功能不变的代码重构
  • perf: 优化性能的代码更改
  • test:测试
  • style: 代码格式化
  • build: 影响构建或外部依赖的更改(gulp, npm,yarn)
  • chore:构建过程或辅助工具的变动
  • ci: 更改CI配置文件和脚本(Travis、Circle、BrowserStack、SauceLabs)
  • docs:项目文档
  • revert: 恢复前一个提交

scope(范围)

当前 commit 影响的代码范围,如:
后端: controller, service, repository
前端: view,api,component

subject

是提交的简短描述,以动词开头

body

是提交的详细描述

  1. 描述不兼容变化
BREAKING CHANGE: [变化]

[变化前后示例]
  1. 关闭的 issue
Closes #231

工具

Commitizen

是生成约定式提交的工具

安装

npm install -g commitizen
# 或
yarn global add commitizen

在项目目录下初始化提交约定

# npm
commitizen init cz-conventional-changelog --save --save-exact

# yarn
commitizen init cz-conventional-changelog --yarn --dev --exact

提交时用 git cz 代替 git commit

@commitlint/cli

检测提交是否符合约定

安装

npm install --save-dev @commitlint/cli @commitlint/config-angular
# windows
npm install --save-dev @commitlint/config-conventional @commitlint/cli

// 在项目下配置
echo "module.exports = {extends: ['@commitlint/config-angular']};" > commitlint.config.js

在git提交前检查

# 安装 Husky
npm install husky --save-dev
# 或
yarn add husky --dev

# 激活 hooks
npx husky install
# 或
yarn husky install

# 添加 hook
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'

standard-version

生成提交历史工具

安装

npm install -g standard-version

在项目下运行 standard-version,会生成 CHANGELOG.md 文件,包含全部提交历史

一键脚本

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

git-commit-message-helper

jetbrains 相关编辑器下的插件

原文地址:https://www.cnblogs.com/plum-nikolas/p/15230796.html