如何将Git的分支名称添加到提交消息?

我需要一些Bash脚本的帮助,它会自动添加git的分支名称作为提交消息中的散列。

使用prepare-commit-msg或者commit-msg githook 。

您的PROJECT/.git/hooks/目录中已经有示例。

作为一种安全措施,您将不得不在每个希望使用它的存储库上手动启用这样的挂钩。 虽然,您可以提交脚本并将其复制到.git/hooks/目录.git/hooks/所有克隆中。

这里是我的commit-msg脚本作为例子:

 #!/bin/sh # # Automatically adds branch name and branch description to every commit message. # NAME=$(git branch | grep '*' | sed 's/* //') DESCRIPTION=$(git config branch."$NAME".description) echo "$NAME"': '$(cat "$1") > "$1" if [ -n "$DESCRIPTION" ] then echo "" >> "$1" echo $DESCRIPTION >> "$1" fi 

创build以下提交消息:

 [branch_name]: [original_message] [branch_description] 

我使用issue number作为branch_name ,使用git branch --edit-description [branch_name]命令将问题描述放置到branch_description

更多关于分行描述,你可以在这个问答中find。

代码示例存储在以下Gist中 。

一个简单的脚本, 编辑之前将分支名称添加到提交消息中。 所以,如果你想要改变或删除它,你可以。

创build这个文件.git / hooks / prepare-commit-msg

 #!/bin/bash branchPath=$(git symbolic-ref -q HEAD) #Somthing like refs/heads/myBranchName branchName=${branchPath##*/} #Get text behind the last / of the branch path firstLine=$(head -n1 $1) if [ -z "$firstLine" ] ;then #Check that this is not an amend by checking that the first line is empty sed -i "1s/^/$branchName: \n/" $1 #Insert branch name at the start of the commit message file fi 

你可以用prepare-commit-msg和pre-commit钩子的组合来完成。

的.git /钩/准备提交-MSG

 #!/bin/sh BRANCH=`git branch | grep '^\*' | cut -b3-` FILE=`cat "$1"` echo "$BRANCH $FILE" > "$1" 

的.git /钩/预提交

 #!/bin/bash find vendor -name ".git*" -type d | while read i do if [ -d "$i" ]; then DIR=`dirname $i` rm -fR $i git rm -r --cached $DIR > /dev/null 2>&1 git add $DIR > /dev/null 2>&1 fi done 

设置权限

 sudo chmod 755 .git/hooks/prepare-commit-msg sudo chmod 755 .git/hooks/pre-commit 

在prepare-commit-msg文件中添加下面的代码。

 #!/bin/sh # # Automatically add branch name and branch description to every commit message except merge commit. # COMMIT_EDITMSG=$1 addBranchName() { NAME=$(git branch | grep '*' | sed 's/* //') DESCRIPTION=$(git config branch."$NAME".description) echo "[$NAME]: $(cat $COMMIT_EDITMSG)" > $COMMIT_EDITMSG if [ -n "$DESCRIPTION" ] then echo "" >> $COMMIT_EDITMSG echo $DESCRIPTION >> $COMMIT_EDITMSG fi } MERGE=$(cat $COMMIT_EDITMSG|grep -i 'merge'|wc -l) if [ $MERGE -eq 0 ] ; then addBranchName fi 

它会添加分支名称来提交消息,除了merge-commit。 合并提交默认情况下有分支信息,所以额外的分支名称是不必要的,使消息变得丑陋。

由Tim的答案启发,build立在最上面的答案,事实certificateprepare-commit-msg钩子作为参数是什么样的提交正在发生 。 如默认的prepare-commit-msg所示,如果$ 2是“合并”,那么它是一个合并提交。 因此,case switch可以被修改为包含Tim的addBranchName()函数。

我已经包括了我自己的偏好,以便如何添加分支名称,以及默认prepare-commit-msg.sample钩子的所有未注释部分。

准备提交-MSG

 #!/bin/sh addMyBranchName() { # Get name of current branch NAME=$(git branch | grep '*' | sed 's/* //') # First blank line is title, second is break for body, third is start of body BODY=`cut -d \| -f 6 $1 | grep -v -E .\+ -n | cut -d ':' -f1 | sed '3q;d'` # Put in string "(branch_name/): " at start of commit message body. # For templates with commit bodies if test ! -z $BODY; then awk 'NR=='$BODY'{$0="\('$NAME'/\): "}1;' $1 > tmp_msg && mv tmp_msg "$1" else echo "title\n\n($NAME/):\n`cat $1`\n" > "$1" fi } # You might need to consider squashes case "$2,$3" in # Commits that already have a message commit,?*) ;; # Messages are one line messages you decide how to handle message,) ;; # Merge commits merge,) # Comments out the "Conflicts:" part of a merge commit. perl -i.bak -ne 's/^/# /, s/^# #/#/ if /^Conflicts/ .. /#/; print' "$1" ;; # Non-merges with no prior messages *) addMyBranchName $1 ;; esac