git commit 工具 - commitizen

为了规范代码提交,最近学习了一下如何使用commitizen进行commit提交,其实也很简单,这里简单做个介绍。

commitizen 安装

npm install -g commitizen

使用

对于并非使用nodejs的项目,要先通过

npm init --yes

完成初始化,然后通过

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

启用commitizen功能,此后使用git cz替换git commit即可。当然为了方便可以在.bashrc.zshrc中添加一个别名。

alias cz="commitizen init cz-conventional-changelog --save --save-exact"

然后source ~/.bashrc or source ~/.zshrc 即可。好了,下面是我的笔记库的应用实例。

➜  Notes git:(master) ✗ npm init --yes
➜  Notes git:(master) ✗ commitizen init cz-conventional-changelog --save --save-exact
➜  Notes git:(master) ✗ git cz
cz-cli@3.0.7, cz-conventional-changelog@2.1.0


Line 1 will be cropped at 100 characters. All other lines will be wrapped after 100 characters.

? Select the type of change that you're committing:
  chore:    Other changes that don't modify src or test files
  revert:   Reverts a previous commit
  feat:     A new feature
❯ fix:      A bug fix
  docs:     Documentation only changes
  style:    Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
  refactor: A code change that neither fixes a bug nor adds a feature
(Move up and down to reveal more choices)

目前该工具支持以下几种提交类型:

type description
feat A new feature
fix A bug fix
docs Documentation only changes
style Changes that do not affect the meaning of the code
refactor A code change that neither fixes a bug nor adds a feature
perf A code change that improves performance
test Adding missing tests or correcting existing tests
build Changes that affect the build system or external dependencies
ci Changes to our CI configuration files and scripts
chore Other changes that don't modify src or test files
revert Reverts a previous commit

下面是添加commit的过程

➜  Notes git:(master) ✗ git cz
cz-cli@3.0.7, cz-conventional-changelog@2.1.0


Line 1 will be cropped at 100 characters. All other lines will be wrapped after 100 characters.

? Select the type of change that you're committing: docs:     Documentation only changes
? What is the scope of this change (e.g. component or file name)? (press enter to skip)

? Write a short, imperative tense description of the change:
 update docs about git stash
? Provide a longer description of the change: (press enter to skip)

? Are there any breaking changes? No
? Does this change affect any open issues? No
[master d82e0e9] docs: update docs about git stash
 2 files changed, 19 insertions(+)
➜  Notes git:(master)

提交过程会逐步提示你添加相应的内容,不添加的直接回车跳过即可。大部分情况只要选择type以及描述信息。

参考