Git項目提交規范結合Husky + commitlint使用


一、前置條件

  為了更好地 GIT 提交,加入了代碼提交規范和規范校驗,優雅的提交;

  方便團隊協作和快速定位問題,采取 Husky + commitlint 輔助項目做約定。

   npm install --save-dev husky  

  For windows install commintlint:

   npm install --save-dev @commitlint/config-conventional @commitlint/cli  

 

二、配置

  // 命令生成配置文件 commitlint.config.js 或 .commitlintrc.js

   echo "module.exports = {extends: ['@commitlint/config-conventional']};" > commitlint.config.js   

  package.json 添加commitlint配置項:

"husky": {
    "hooks": {
      "pre-commit": "npm run lint",
      "commit-msg": "commitlint -e $HUSKY_GIT_PARAMS"
    }
}

  husky是git hook工具,文件格式 .huskyrc,它能幫你阻擋不好的代碼提交和推送;

  (pre-commit 鈎子命令用於提交前運行檢查,看項目情況決定要不要使用,

   "pre-commit": "lint-staged"  擴展使用lint-staged庫輔助,或者是自定義lint,

   "lint": "eslint src --fix --ext .ts,.tsx "  

         

  或者創建文件 .lintstagedrc  

  (如果無pre-commit限制,使用 --no-verify  eg: git commit --no-verify -m 'feat: 增加 xxx 功能'  )

 

三、定制提交規范

  1.提交格式(冒號后面有空格):

    '<type>[scope]: <subject>'
  // eg: git commit -m 'feat: 增加什么功能'

    type|subject 必選,scope 可選;

    scope 可省略,用於說明commit的影響范圍和模塊;

    subject 是commit的目的簡短描述,可以配置最大長度限制,配置72字符;

  2.常用type類型

'build' 

項目構建的提交(eg:webpack配置等)
'upd'  更新某個功能
'feat' 新功能(feature)
'fix' 修補bug

'refactor'

重構代碼(不是新增也不是修補代碼)
'style' 不影響程序邏輯的改動(eg:格式)
'perf' 性能優化
'revert' 回滾到某個更早的提交

'docs'

文檔更新

'chore' 

其他類型(eg:構建過程或輔助工具的變化)

'test'

增加測試用例

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  3. commitlint config rules

    level: 0為disable,1為warning,2為error;

     第二位: 'always'或'never';

     第三位: 值

  4.配置圖如下:

  

 

四、范例

  終端(開發分支)步驟:

  git add . -> git commit –no-verify –m ‘upd: 更新某個功能’-> git push

  github desktop /vscode git管理/TortoiseGit客戶端:

  暫存更改 -> 描述(upd: 更新某個功能) commit  ->  push/fetch

  

 


免責聲明!

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



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