커밋 메시지에 Git의 브랜치 이름을 추가하는 방법은 무엇입니까?
커밋 메시지에 git의 브랜치 이름을 해시로 자동 추가하는 Bash 스크립트에 대한 도움이 필요합니다.
prepare-commit-msg
또는 commit-msg
githook을 사용하십시오 .
PROJECT/.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
다음 커밋 메시지를 생성합니다.
[branch_name]: [original_message]
[branch_description]
문제 번호를으로 사용하고 branch_name
있으며 문제 설명은 branch_description
using git branch --edit-description [branch_name]
명령에 배치됩니다 .
지점 설명에 대한 자세한 내용은이 Q & A 에서 찾을 수 있습니다 .
코드 예제는 다음 Gist에 저장됩니다 .
편집 하기 전에 커밋 메시지에 분기 이름을 추가하는 좀 더 간단한 스크립트입니다 . 따라서 변경하거나 제거하려면 할 수 있습니다.
.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 / hooks / prepare-commit-msg
#!/bin/sh
BRANCH=`git branch | grep '^\*' | cut -b3-`
FILE=`cat "$1"`
echo "$BRANCH $FILE" > "$1"
.git / hooks / pre-commit
#!/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을 제외한 커밋 메시지에 브랜치 이름을 추가합니다. merge-commit에는 기본적으로 분기 정보가 있으므로 추가 분기 이름이 필요하지 않으며 메시지를보기 흉하게 만듭니다.
Inspired by Tim's answer which builds upon the top answer, it turns out the prepare-commit-msg hook takes as an argument what kind of commit is occurring. As seen in the default prepare-commit-msg if $2 is 'merge' then it is a merge commit. Thus the case switch can be altered to include Tim's addBranchName() function.
I've included my own preference for how to add the branch name, and all the uncommented parts of the default prepare-commit-msg.sample
hook.
prepare-commit-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
If you want to make it global (for all projects):
Create git-msg
file with the content of shytikov's answer, and put it in some folder:
mkdir -p ~/.git_hooks
# make it executable
chmod a+x ~/.git_hooks/commit-msg
Now enable hooks:
git config --global init.templatedir '~/.git_hooks'
and git init
again in each project you want to use it.
I was having issues getting these solutions to work on MacOS due to the fact that it uses BSD sed
instead of GNU sed
. I managed to create a simple script that does the job though. Still using .git/hooks/pre-commit
:
#!/bin/sh
BRANCH=$(cat .git/HEAD | cut -d '_' -f2)
if [ ! -z "$BRANCH" ]
then
echo "$BRANCH" > "/Users/username/.gitmessage"
else
echo "[JIRA NUMBER]" > "/Users/username/.gitmessage"
fi
This assumes a branch naming standard similar to functional-desc_JIRA-NUMBER
. If your branch name is only your Jira ticket number you can simply get rid of everything from the pipe to the f2. It also requires that you have a file named .gitmessage
in your home directory.
There is no reason to grep then pipe to sed.
As our branches are our jira issue, we always have to prepend our jira to the commit message (which is the same as the branch name).
$ git commit -m "$(git branch | sed -n 's/\* //p'): message"
참고URL : https://stackoverflow.com/questions/5894946/how-to-add-gits-branch-name-to-the-commit-message
'code' 카테고리의 다른 글
외부 어셈블리에서 내부 클래스에 액세스하려면 어떻게해야합니까? (0) | 2020.08.31 |
---|---|
사용자 정의 대화 상자 애니메이션 (0) | 2020.08.31 |
5 초마다 Javascript 함수를 지속적으로 호출 (0) | 2020.08.31 |
데이터 세트에서 특이 치를 제거하는 방법 (0) | 2020.08.31 |
priorityQueue를 최대 priorityqueue로 변경 (0) | 2020.08.31 |