git commit规范化实践

最近从svn转到git进行代码版本控制,今天了解了git commit规范化的一些知识后,写此文章记录下配置过程。

环境

编辑器使用的是vscode,项目框架是vue3.0

规范化工具

规范化git commit消息的工具commitizen

 # 将commitizen命令行安装到全局
npm install -g commitizen

安装完之后我们就项目中引进改工具,就可以规范commit行为了。提交的命令为git cz,如果还是用git commit命令进行提交,那么这个工具就不会起到什么作用了。

commitizen对commit规范化界面都是英文提示,这个时候我就想如果要汉化怎么办,这就有了下面一个工具的出现。

安装可定制的Commitizen插件cz-customizable

npm install cz-customizable --save-dev

安装cz-customizable可以配置自定义的commitizen配置文件,在自定义的配置文件中我就可以对配置进行汉化

  "config": {
    "commitizen": {
      "path": "./node_modules/cz-customizable"
    },
    "cz-customizable": {
      "config": "my.cz-config.js" // 这里的文件名可以自定义,但是改文件需要放置在项目的根目录下
    }
  }

汉化完之后的效果是下面这样:

版本发布

进行commit规范化的好处是为了提高团队协作效率,使代码阅读性更强。还有另外一个节省后期维护版本信息的成本。通过规范化commit行为,我们可以通过自动化工具生成版本信息这样极大的降低了维护成本,提高了工作效率。在这里我使用的版本发布工具是standard-version,当然还有conventional-changelog可以使用。

standard-version可以自动帮助我们做以下几件事情:

  1. 自动在数据中生成版本号
  2. 使用conventional-changelog更新 CHANGELOG.md
  3. 提交package.json (如果有) 和 CHANGELOG.md
  4. 给新版本打一个tag

首先是安装standard-version

npm i standard-version --save-dev

安装完成之后,执行standard-version命令,在控制台可以看到如下信息:

可以清楚的看到standard-version做了哪些事情。其中package.json和changelog.md文件是被自动提交了的。这样在项目中生成了一个changelog文件

# Changelog

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [0.3.2](http://112.80.40.2:11080/chenchent/cdss-acs/compare/v0.3.1...v0.3.2) (2019-07-25)



### [0.3.1](http://112.80.40.2:11080/chenchent/cdss-acs/compare/v0.3.0...v0.3.1) (2019-07-25)


### Bug Fixes

* 将commitizen使用本地配置文件 ([4accd0a](http://112.80.40.2:11080/chenchent/cdss-acs/commit/4accd0a))



## [0.3.0](http://112.80.40.2:11080/chenchent/cdss-acs/compare/v0.2.0...v0.3.0) (2019-07-25)

### Bug Fixes

- **changelog:** 生成新的 changelog 文件 ([17747cf](http://112.80.40.2:11080/chenchent/cdss-acs/commit/17747cf))

### BREAKING CHANGES

- **changelog:** 测试

## 0.2.0 (2019-07-25)

### Features

- **me:** 测试 ([64e596d](http://112.80.40.2:11080/chenchent/cdss-acs/commit/64e596d))

# 0.1.0 (2019-07-25)

### Features

- **me:** 测试 ([64e596d](http://112.80.40.2:11080/chenchent/cdss-acs/commits/64e596d))

由于我习惯于使用node命令,所以最后我又将规范化跟版本生成的命令配置到了scripts中

  "scripts": {
    "commit": "git cz",
    "changelog": "standard-version --dry-run && standard-version" 
# standard-version --dry-run只是用来打印要做的事情,并不会做实际的操作
  }

这就是我对git commit规范化的一些实践。mark下来做一个记录,希望可以帮助到其他人。

posted @ 2019-07-25 16:46  寻找石头鱼  阅读(1188)  评论(1编辑  收藏  举报