git commit 代碼提交規範

git commit 代碼提交規範

1、爲何須要制定提交規範?

在團隊協做開發時,每一個人提交代碼時都會寫 commit message。webpack

每一個人都有本身的書寫風格,翻看咱們組的git log, 能夠說是五花八門,十分不利於閱讀和維護。git

通常來講,大廠都有一套的本身的提交規範,尤爲是在一些大型開源項目中,commit message 都是十分一致的。github

所以,咱們須要制定統一標準,促使團隊造成一致的代碼提交風格,更好的提升工做效率,成爲一名有追求的工程師。web

2、業界通用的 git 提交規範有哪些?

1. commitizen

AngularJS 在 github上 的提交記錄被業內許多人承認,逐漸被你們引用。npm

格式:

type(scope) : subject

( 1 ) type(必須) : commit 的類別,只容許使用下面幾個標識:gulp

  • feat : 新功能
  • fix : 修復bug
  • docs : 文檔改變
  • style : 代碼格式改變
  • refactor : 某個已有功能重構
  • perf : 性能優化
  • test : 增長測試
  • build : 改變了build工具 如 grunt換成了 npm
  • revert : 撤銷上一次的 commit
  • chore : 構建過程或輔助工具的變更

( 2 ) scope(可選) : 用於說明 commit 影響的範圍,好比數據層、控制層、視圖層等等,視項目不一樣而不一樣。vim

( 3 ) subject(必須) : commit 的簡短描述,不超過50個字符。
commitizen 是一個撰寫合格 Commit message 的工具,
遵循 Angular 的提交規範。性能優化

安裝:

全局安裝 commitizen編輯器

npm install -g commitizen

進入項目文件夾,運行以下命令:grunt

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

使用:

git cz 命令取代 git commit(先使用git add),這時會出現以下選項:

( 1 )選擇 type

( 2 )填寫 scope(選填)

? What is the scope of this change (e.g. component or file name)? (press enter to skip)
core

( 3 )填寫 subject

? Write a short, imperative tense description of the change:
set a to b

完成,運行 git log 命令,查看咱們剛纔提交的 commit message,以下:

fix(core): set a to b

優勢:

  • 符合業內標準(許多項目使用 AngularJS 的commit 規範)
  • 提交過程更加規範(使用 commitizen 規範工具,風格統一)
  • 可以生成風格統一的 commit log(type(scope):subject)

缺點:

  • 須要安裝 commitizen 工具包,使項目更大、更重了(適合大型開源項目)
  • 提交過程受約束較大
  • 有必定的學習成本

2. 設置 git commit 模板

步驟以下:

( 1 ) 創建模板文件

在項目中創建 .git_template 文件,內容能夠自定義:

type:
scope:
subject:

( 2 ) 設置模板

運行以下命令:

git config commit.template .git_template // 當前項目
<!-- git config commit.template .git_template // 全局設置 -->

( 3 ) 提交代碼

先使用 git add 添加代碼
使用 git commit 按照模板填寫
最後 git push 推送到遠端

優勢:

  • 規則可配置,更自由
  • 配置方式簡潔(只需添加配置文件)

缺點:

  • 便利性差,每次都要用 vim 編輯器填寫模板
  • 易出錯,沒有可靠的校驗方式

3、制定適合咱們的 git commit 提交規範

第二章中提到的兩種業內廣泛使用的規範,都不徹底適合咱們。

第一種方式適合大型開源項目,咱們若是也照搬會比較麻煩,但咱們能夠借鑑 type(scope): subject 的提交格式,也算是與大廠同步;
第二種方式雖然自由,可是也不比較麻煩,要配置模板。
所以,咱們只模仿 type(scope): subject 的提交格式,不使用工具 or 模板校驗,靠你們自覺遵照便可。

格式

type: description

1. type 類型

type 是 commit 的類別,只容許以下幾種標識:

  • fix: 修復bug
  • add: 新功能
  • update: 更新
  • style : 代碼格式改變
  • test: 增長測試代碼
  • revert: 撤銷上一次的commit
  • build: 構建工具或構建過程等的變更,如:gulp 換成了 webpack,webpack 升級等

2. description

description 是對本次提交的簡短描述。

不超過50個字符。

推薦以動詞開頭,如: 設置、修改、增長、刪減、撤銷等

最後,既然制定了規則,你們就遵照起來吧~~~~

相關文章
相關標籤/搜索