Git Commit強制規范(commitlint+husky)


從我的notion文檔遷移到的博客園:[https://www.notion.so/Git-Commit-commitlint-husky-9cc381b35484437ca32f474cda40dc24)

Git Commit強制規范(commitlint+husky)

一、緣起

  • 規范前

  • 規范后

二、工具介紹

1、commitlint

commitlint 是當前使用最為廣泛的 git commit 校驗約束工具之一,

commitlint helps your team adhering to a commit convention. By supporting npm-installed configurations it makes sharing of commit conventions easy.

(1)安裝

npm install -g @commitlint/cli @commitlint/config-conventional

config-conventional是社區整理的常用的Commit規范,見如下

https://www.npmjs.com/package/@commitlint/config-conventional

(2)配置

生成commitlint.config.js配置文件,命令如下:

echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js
  • 默認采用config-conventional:

    module.exports = {extends: ['@commitlint/config-conventional']};
    
  • 可自定義配置:

    配置規則見 https://commitlint.js.org/#/reference-configuration

    module.exports = {
        extends: ['@commitlint/config-conventional'],
        rules: {
            'type-enum': [2, 'always', [
                "feat", "upd", "del", "fix", "refactor", "test", "perf", "docs", "style", "revert"
            ]],
            'subject-full-stop': [0, 'never'],
            'subject-case': [0, 'never']
    
        }
    };
    

2、husky

Husky can prevent bad git commit, git push and more 🐶 woof!

husky 是一個增強的 git hook 工具,可以在 git hook 的各個階段執行我們在 package.json 中配置好的 npm script。

借助husky在每次 commit 時執行 commitlint來檢查我們輸入的 message。

(1)安裝

注意:指定-g也不對所有Project生效!每個Project都需要重新安裝husky

npm install husky -g

(2)配置

在 package.json 中配置 husky,commit-msg指定為commitlint (將在git hook的commit-msg階段調用commitlint )

"devDependencies": {
...
},
"husky": {
    "hooks": {
      "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
    }
  }

三、實踐

1、VS Code

針對VS Code的Commit需要先配置global user.name和user.email(並不是都需要設置,有時為VS Code錯誤提示導致)

$  git config --global user.name "輸入你的用戶名"
$  git config --global user.email "輸入你的郵箱"
  • 不符合規范的提示

    git commit -m 'test'

  • 規范的Commit

    git commit -m "feat: 新功能"

2、Git Bash

  • 不符合規范的提示

    git commit -m 'test'

  • 規范的提示

    git commit -m "fix: 修改test.cls"

四、注意

1、格式

?號對應可選項

type(scope?): subject
body?
footer?

2、Type約定

'feat', // 新功能
'upd', // 修改
'del', // 刪除
'fix', // bug修復
'test', // 單元測試
'perf', // 性能優化
'docs', // 文檔更新
'style', // 樣式變動
'refactor', // 功能重構
'revert', // 回滾某個更早之前的提交
'package', // 創建包

3、Commit husky報錯

husky > commit-msg hook failed (add --no-verify to bypass)

  • 詳細錯誤見下圖:

  • 解決方法

    commitlint.config.js的編碼修改為UTF-8

4、npm install 命令

注意-g -save-dev

NPM install -save 和 -save-dev 傻傻分不清

npm-install

五、背后的原理

1、Git Hooks

某些Action如commit、merge發生時,可觸發自定義腳本。觸發動作可以在客戶端或服務器端。

Like many other Version Control Systems, Git has a way to fire off custom scripts when certain important actions occur. There are two groups of these hooks: client-side and server-side. Client-side hooks are triggered by operations such as committing and merging, while server-side hooks run on network operations such as receiving pushed commits. You can use these hooks for all sorts of reasons.

https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks

2、commit.template

提交模板,對於Commit可以自定義一套提交后的提示內容及格式,可以結合Git Hooks

If you set this to the path of a file on your system, Git will use that file as the default initial message when you commit. The value in creating a custom commit template is that you can use it to remind yourself (or others) of the proper format and style when creating a commit message.

set the commit.template configuration value:

$ git config --global commit.template ~/.gitmessage.txt
$ git commit

https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration

六、參考

commitlint

https://commitlint.js.org/

config-conventional

https://www.npmjs.com/package/@commitlint/config-conventional

Husky

https://github.com/typicode/husky

你可能已經忽略的 git commit 規范

https://mp.weixin.qq.com/s/8oWsj_ipp73crD_vg58LeQ

git-commit-lint-vscode

https://github.com/UvDream/git-commit-lint-vscode

Git

https://git-scm.com/about


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM