超级好用的Git Hooks使用指南

超级好用的Git Hooks使用指南

之前有写过一篇如何优雅写 Git Commit 的文章,但是如果真的要每次提交都注意这些规范,未免有些繁琐。作为一个程序猿,能用工具解决当然是要使用工具的,所以今天我们就来介绍一下Commitizen

Commitizen

Commitizen is an open source project that helps contributors be good open source citizens. It accomplishes this by prompting them to follow commit message conventions at commit time. It also empowers project maintainers to create or use predefined commit message conventions in their repos to better communicate their expectations to potential contributors.

安装 Commitizen

npm install -g commitizen
## 或 cnpm npm install -g commitizen
## 或 yarn global add commitizen

commitizen安装完成后,我们还并不能在Terminal中直接使用它,我们还需要一个命令行工具git-cz

commitizenGitHub上有说明如何使用这个命令,但是我觉得太麻烦了,取而代之使用的是emoji-cz

安装 emoji-cz

cnpm install -g emoji-cz
# yarn global add emoji-cz
# OR
# npm install --global emoji-cz

set as default adapter globally

echo '{ "path": "emoji-cz" }' > ~/.czrc

目前为止,安装工作已经完成了,接下来就可以直接使用。

使用

Simply use git cz instead of git commit when committing. See the doc of Commitizen for more info.

git-cz

设置

最后,我们还可以通过配置文件进行一些个性化配置。总共有三种方法:

  1. package.json
  2. .cz.json
  3. .czrc
// in package.json
// 没有找到在哪里配置
"config": {
  "commitizen": {
    // ...
    "emoji-cz": {
      // Overwrite types prompted to the command line.
      "types": {
        "Fix": {
          "emoji": "🐝", // overwrite "Fix" emoji to a bee
          "name": "Bug", // overwrite "Fix" name to "Bug"
          "description": "Dirty bug" // overwrite description of "Fix"
        },
        // add a new type "Chore"
        "Chore": {
          "emoji": "❓",
          "description": "Other changes that don't modify src or test files"
        }
      },

      // Overwrite the output commit subject in the specified format.
      // Below is the default format,
      // [emoji] will be replace with the chose type's emoji,
      // [name] will be replace with the chose type's name,
      // [subject] will be replace with the subject you entered.
      // One example output of the format can be: `✨ Feat: initial commit`
      "format": "[emoji] [name]: [subject]"
    }
  }
}

// in .cz.json or .czrc  
// 这个文件位于 ~/.czrc
{
  "emoji-cz": {
    //...
  }
}

这些配置主要配置在使用了git cz之后,提交的选项内容。

送上一份自用的.czrc

使用愉快

Copyright: 采用 知识共享署名4.0 国际许可协议进行许可

Links: https://baozi.fun/2020/08/26/use-with-git-cz

Buy me a cup of coffee ☕.