1. 程式人生 > 實用技巧 >Git Hook - 實現commit-msg自動裝飾

Git Hook - 實現commit-msg自動裝飾

背景:並行版本較多,合併至版本釋出分支主分支時無法看出初始提交分支

需求:系統開發人員執行git commit時,自動在commit-msg中補充當前所在分支名

操作步驟:

1、在子系統git倉庫路徑/.git/hooks下,將以下內容覆蓋commit-msg.sample檔案中

#!/bin/sh
#
# An example hook script to check the commit log message.
# Called by "git commit" with one argument, the name of the file
# that has the commit message.  The hook should exit with non
-zero # status after issuing an appropriate message if it wants to stop the # commit. The hook is allowed to edit the commit message file. # # To enable this hook, rename this file to "commit-msg". branch_name=`git symbolic-ref --short -q HEAD` echo "${branch_name}" commit="<"${branch_name}">
"$(cat $1) echo "$commit" > "$1"

2、去掉檔名字尾.sample使其生效

在git-bash中執行commit操作後,通過git log檢視提交記錄,此時提交資訊已成功生效,顯示內容為:<分支名>提交資訊