[TOC]
# git commit 提交規范 & 規范校驗
## Git Log 之痛
在《The Art of Readable Code》這本經典書中,有個形象的比喻,衡量代碼可讀性的指標是閱讀代碼時每分鐘的 WTF 次數,而在讀 Git 提交歷史的時候,不知道你有多少次爆粗口?不相信?你現在打開公司演進最快的項目,執行 git log,信息量過少甚至是誤導的 commit message 非常常見,比如:
```
fix => 這到底是 fix 什么?為什么 fix?怎么 fix 的?
update => 更新了什么?是為了解決什么問題?
test => 這個最讓人崩潰,難道是為了測試?至于為了測試而去提交一次代碼么?
```
說不定,你在這種 commit message 中也貢獻了一份力量呢。
## 修改git message默認編輯器
說了這么多寫作規范,如果有個模板可以直接拷貝,是再好不過的。
可以把默認編輯器改成 vim:
```
git config –global core.editor vim
```
## commit messages 模板
```
curl https://gist.githubusercontent.com/keniushadu/5c7de811bfd77c4cd9c5cd981d401d0d/raw/e048f777bd690767c13544b63f936c1a3219da5e/commit-msg.txt >> ~/.git-commit-template.txt
```
標準模板內容:
```
# Type(<scope>): <subject>
# <body>
# <footer>
# type 字段包含:
# feat:新功能(feature)
# fix:修補bug
# docs:文檔(documentation)
# style: 格式(不影響代碼運行的變動)
# refactor:重構(即不是新增功能,也不是修改bug的代碼變動)
# test:增加測試
# chore:構建過程或輔助工具的變動
# scope用于說明 commit 影響的范圍,比如數據層、控制層、視圖層等等。
# subject是 commit 目的的簡短描述,不超過50個字符
# Body 部分是對本次 commit 的詳細描述,可以分成多行
# Footer用來關閉 Issue或以BREAKING CHANGE開頭,后面是對變動的描述、
# 以及變動理由和遷移方法
# 英文版詳細例子,請看這里
# https://github.com/sparkbox/how_to/tree/master/style/git
# 中文版介紹,請看這里
# http://www.ruanyifeng.com/blog/2016/01/commit_message_change_log.html
```
```
git config --global commit.template ~/.git-commit-template.txt
```
## 簡單模板內容
```
如 xxx_template文件,其內容如下:
[需求/BUG]:
[模塊]:
[說明]:
```
## 注意
a、`git commit` 之前先要使用 `git add` 將沒有入庫的代碼入庫
b、可以使用 `git add -a` 提交多個代碼文件
5、在 `git commit` 之后,會以設置的編輯器打開模板文件,然后按照格式修改該文件,并保存
> 與團隊成員約定做 git 提交時使用 `git commit`,不能直接用 `git commit -m` 跳過規范。
## 規范校驗
在多人協作項目中,如果代碼風格統一、代碼提交信息的說明準確,那么在后期協作以及Bug處理時會更加方便。
因此,在本文章中,我會介紹怎么使用下面這個工具,在 `git push` 代碼之前檢測 `commit messages`:
* [commitlint
](https://commitlint.js.org/)
* husky
# Refs
[Semantic Release with Lerna and Conventional Commits](https://michaljanaszek.com/blog/lerna-conventional-commits)
[GitHub中使用 issues 模版和 pull request 模版](https://www.jianshu.com/p/39e7e3c0a43f)
[About issue and pull request templates](https://help.github.com/en/articles/about-issue-and-pull-request-templates)
[Commit message 和 Change log 編寫指南](http://www.ruanyifeng.com/blog/2016/01/commit_message_change_log.html)
[你可能已經忽略的git commit規范](https://zhuanlan.zhihu.com/p/100574040)
- 介紹
- 1. Get Git - 安裝并且設置Git
- 2. Repository - 建立一個本地的repository
- 3. Commit to it - 檢查狀態、新增或修改commits
- 4. GitHubbin - 注冊GitHub帳號
- 5. Remote Control - 將repository做本地和遠程的連接
- 6. Forks and Clones - Fork和clone一個開源的計劃
- 7. Branches aren't just for Birds - 建立一個feature branch
- Git分支開發模型
- 8. It's a Small World - 邀請并和別人合作
- 9. Pull, Never Out of Date - 利用Push和pull來和GitHub.com同步
- 10. Requesting You Pull Please - 建立一個pull request
- 11. Merge Tada - Merge和刪除branches
- 學會 GitHub
- GitHub 使用指南
- git 與 github 模板配置
- GitHub Actions
- 實用技巧
- Git Flow
- Submodule子模塊
- 工作記錄
- 常用命令
- 幫助
- 資源
- 解疑答惑