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查看提交記錄,此時提交信息已成功生效,顯示內容為:<分支名>提交信息


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM