使用 git commit template 管理 git log
身為開發者,把 commit 線圖弄得漂亮也是蠻有成就感的。可是當團隊裡每個人都有自己的 commit log 風格,這看起來真的是阿雜。
所以在團隊開發時,可以訂定一個 commit template 讓大家去遵守,除了線圖會比較乾淨整齊外,需要回頭找 commit 的時候也必較容易。
最無腦的方式,就是你把 msg text 貼給大家,大家每次要 commit 的時候用 copy paste 的方式自行貼上修改,但這真的蠻蠢的對吧!還好,git config 提供幫你達到統一 msg 的功能。
管理 commit log
目的:統一團隊之中的 commit message 格式。
- 新增 .gitmessage.txt (內容可參考 .gitmessage 範例)
touch .gitmessage.txt
2. 複製檔案到 ~/
資料夾
(你要放在其他地方也行,再下一步指定到對的位置就好)
cp .gitmessage.txt ~/.gitmessage.txt
3. 設定 git config 中 template 的位置
git config --global commit.template ~/.gitmessage.txt
如果是使用 terminal 下指令,默認情況下會自動忽略以 # 開頭的行數。這是因為編輯器編寫 commit 的規則。所以當你用 terminal 執行 git commit 時,你會看見有段文字告訴你,# will be ignored.
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
# new file: what
#
但你用其他 IDE,例如 SourceTree 會發現 # 的行數內容也會一起提交出去。可以設定 commit.cleanup
git config --global --add commit.cleanup strip
文檔是這麼寫的
--cleanup=<mode>
This option sets how the commit message is cleaned up. The <mode> can be one of verbatim, whitespace, strip, and default. The default mode will strip leading and trailing empty lines and #commentary from the commit message only if the message is to be edited. Otherwise only whitespace removed. The verbatim mode does not change message at all, whitespace removes just leading/trailing whitespace lines and stripremoves both whitespace and commentary.
這樣子,使用 SourceTree 提交 commit 時,# 的行數也會被省略啦
.gitmessage sample
這是我平常使用的格式,可依造團隊需求增減。
# commit log 請遵造以下格式,並注意冒號後面有一個空格
#
# <type>: <subject> {必要}
#
# <body> {非必要}
#
# <footer> {非必要}
#
# 範例
# feat: implementation login api function
#
# finished login module and integration with server login api
#
# Closes OR-xxxx
#
# <Type>
# 請遵守下列標籤
# feat: 新功能
# fix: Bug修復
# docs: 文檔改變
# style: 代碼格式改變
# refactor: 功能重構
# perf: 性能優化
# test: 增加測試代碼
# build: 改變build工具
# ci: 與ci相關的設定
# add: 增加一些跟功能無關的檔案
# 3rd: 增加第三方
#
# <Subject>
# 用來簡要描述影響本次變動,概述即可
#
# <Body>
# 具體的修改訊息,越詳細越好
#
# <Footer>
# 如果是要關閉特定 Issue 或 Bug. 可以使用 Closes PROJECT-1 or Resolves PROJECT-1 or Fixes PROJECT-1
# 具體清參考 https://docs.gitlab.com/ee/user/project/integrations/jira.html
#
檢查 commit log
目的:提交前,檢查 commit 是否符合規範
當團隊內的 commit log 格式已經有了規範,一起遵守有助於幫助建立團隊。BUT 總是會有成員不小心給忘了,或者是不小心打錯格式了。例如半形分號(:)打成全型分號(;),這樣在線圖上很格式就很明顯不一樣了(至少對於我來講很明顯,強迫症發作…)。
所以 git 也提供了一個方法讓你可以對提交前、提交後 … etc 做一些固定的事情。這邊我只針對提交前檢查 commit-msg,如果需要其他的功能可以參考 git hook。
- 新增 commit-msg.sample
// 我使用 bash 語言
#!/usr/bin/env bash
COMMIT_MSG=""
while read line; do // ...
// 將 commit message 一行一行讀進來之後
// 獲取你要的資訊
// 詳細語法請自行查找 COMMIT_MSG="feat: use new module" // for exampledone < $INPUT_FILE// commit log 為空
if [ -z "$COMMIT_MSG" ]; then
echo "Error: commit message can not be empty."
exit 1 // 重要!
fiif [[ "$COMMIT_MSG" =~ $MERGE_PATTERN ]]; then
echo "Merge commit success!"
exit 0elif [[ "$COMMIT_MSG" =~ $ALLOW_PATTERN ]]; then
echo "Commit message format is valid."
exit 0 else
echo "Commit message format error."
exit 1
fi
2. 加到 hook
在 .git
資料夾底下新增 hooks
資料夾
cd ./.git
mkdir hooks
複製 commit-msg.sample 到 hooks
註: 我將 commit-msg.sample 放在 ci
底下
cp ../ci/commit-msg.sample ./hooks/
接著 cd 到 hooks
底下執行
mv commit-msg.sample commit-msg
chmod +x commit-msg
完成後資料夾會有個執行檔
這樣就完成了。
實際操作一下
以 Terminal 舉例執行提交 commit 並輸入feat:add test
。
我將分號打成全形,而我的 hooks 有去判斷全形是不被接受的。送出提交後,可以看到 console 上出現的 error 並中斷我的提交。
相同的,如果使用 SourceTree 也會噴 error。
畢竟要團隊每個人都執行這麼多指令,肯定麻煩。所以你可以把上述指令寫成 script。這樣子,其他人就只需要執行 script 就行囉。
建立 setup.sh
echo "Setup project Setting ..."#git commit log template
cp ci/.gitmessage.txt ~/.gitmessage.txt
git config --global commit.template ~/.gitmessage.txt
git config --global --add commit.cleanup strip
echo "> set up commit msg template success."#git commit log format hook
cd ./.git
mkdir hooks
cp ../ci/commit-msg.sample ./hooks/
cd ./hooks
mv commit-msg.sample commit-msg
chmod +x commit-msg
echo "> set up commit-msg of hooks success."
《補充》git 指令
查看 config
git config --list
移除 config 中的設定
git config --unset --global [name]