規範化 Git 版本提交信息和版本發佈

規範化 Git 版本提交信息和版本發佈

本文講解了如何利用工具 commitizen/cz-cli + commitizen/cz-conventional-changelog + conventional-changelog/standard-version,規範提交信息和版本發佈。

前言

協做開發下不一樣開發人員的 commit 書寫習慣不一樣,查看 commit 記錄時感受就比較亂且不盡詳細。我本身也是,沒有一套合適的 commit 規範約束,每次都只是大概寫個主要概述就提交,當須要返回查看時就看不出太多信息了。同時 CHANGELOG 也是記錄的不盡詳細,以前的手動書寫不能讓咱們查看項目的每一個發行版之間發生的變化。也因此上網查找了下解決方案,感受甚好,有必要總結一下。node

約定式提交規範

約定式提交規範是基於Angular提交準則造成,提交說明的結構以下:git

<類型>[可選的做用域]: <描述>

[可選的正文]

[可選的腳註]

其中,<類型>是爲了向類庫使用者代表其意圖,其可選值爲:github

  • feat: 表示新增了一個功能
  • fix: 表示修復了一個 bug
  • docs: 表示只修改了文檔
  • style: 表示修改格式、書寫錯誤、空格等不影響代碼邏輯的操做
  • refactor: 表示修改的代碼不是新增功能也不是修改 bug,好比代碼重構
  • perf: 表示修改了提高性能的代碼
  • test: 表示修改了測試代碼
  • build: 表示修改了編譯配置文件
  • chore: 無 src 或 test 的操做
  • revert: 回滾操做

[可選的做用域]: 是爲了描述 這次 commit 影響的範圍,好比: route, component, utils, build, api, website, docsweb

<描述>: 這次提交的簡短描述npm

[可選的正文]: 這次提交的詳細描述,描述爲何修改,作了什麼樣的修改,以及開發的思路等等,輸入 \n 換行json

[可選的頁腳]: 主要寫下面2種api

  • Breaking changes: 在可選的正文或腳註的起始位置帶有 BREAKING CHANGE: 的提交,表示引入了破壞性變動(這和語義化版本中的 MAJOR 相對應)。
  • Closed issues: 羅列這次提交修復的 bug,如 fixes issue #110

兩種形式安裝

全局安裝

npm install -g commitizen cz-conventional-changelog
echo '{ "path": "cz-conventional-changelog" }' > ~/.czrc

全局模式下, 須要 ~/.czrc 配置文件, 爲 commitizen 指定 Adapteride

執行工具

git cz

項目級安裝

npm install -D commitizen cz-conventional-changelog

package.json中配置:性能

"script":{"commit":"git-cz"},
"config":{"commitizen":{"path":"node_modules/cz-conventional-changelog"}}

執行

npm run commit

standard-version: 自動生成 CHANGELOG

一樣能夠有 2 種安裝形式,這裏只介紹項目級安裝,接下來的介紹也都以此爲例。

npm i -S standard-version

package.json 配置:

"scirpt":{"release":"standard-version"}

執行

npm run release

若全局安裝,可直接使用 standard-version 命令,其做用等同於npm run release,下面再也不贅述

standard-version 介紹

選項:

--release-as, -r     Specify the release type manually (like npm version <major|minor|patch|1.1.0>) [字符串]
  // major: 1.0.0 -> 2.0.0, minor: 1.0.0 -> 1.1.0, patch : 1.0.0 -> 1.0.1
  --prerelease, -p     make a pre-release with optional option value to specify a tag id [字符串]
  --infile, -i         Read the CHANGELOG from this file                 [默認值: "CHANGELOG.md"]
  --message, -m        Commit message, replaces %s with new version [字符串] [默認值: "chore(release): %s"]
  --first-release, -f  Is this the first release?                          [布爾] [默認值: false]
  --sign, -s           Should the git commit and tag be signed?            [布爾] [默認值: false]
  --no-verify, -n      Bypass pre-commit or commit-msg git hooks during the commit phase [布爾] [默認值: false]
  --commit-all, -a     Commit all staged changes, not just files affected by standard-version [布爾] [默認值: false]
  --silent             Don't print logs and errors                         [布爾] [默認值: false]
  --tag-prefix, -t     Set a custom prefix for the git tag to be created   [字符串] [默認值: "v"]
  --scripts            Provide scripts to execute for lifecycle events (prebump, precommit, [默認值: {}]
  --skip               Map of steps in the release process that should be skipped    [默認值: {}]
  --dry-run            See the commands that running standard-version would run [布爾] [默認值: false]

經常使用發佈命令

// 初次發佈版本
npm run release --first-release
// 添加版本信息和指定發佈版本等級
npm run release -m "Commit message" -r minor

// 確認發佈,npm publish 發佈到 npm
git push --follow-tags origin master && npm publish

參考連接

優雅的提交你的 Git Commit Message

Standard Version

commitizen/cz-cli

相關文章
相關標籤/搜索