sh 如何使用分支名称自动添加git commit

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 如何使用分支名称自动添加git commit相关的知识,希望对你有一定的参考价值。

#!/bin/bash
# Source: https://gist.github.com/bartoszmajsak/1396344#gistcomment-65489
# For a quick install, into your project folder enter this command line:
# 
# ```
# $ mkdir -p .git/hooks/ && curl https://gist.githubusercontent.com/FBente/19c3559c65cf3daa802e921438557370/raw/020c11b769c6f1535b7966c42d4c3fa670ccf3b6/prepare-commit-msg.sh > .git/hooks/prepare-commit-msg && chmod u+x .git/hooks/prepare-commit-msg
# ```
# * get Gist
# * copy into  _.git/hooks/prepare-commit-msg_ file
# * make it executable

# This way you can customize which branches should be skipped when
# prepending commit message. 
if [ -z "$BRANCHES_TO_SKIP" ]; then
  BRANCHES_TO_SKIP=(master develop test integration archive release)
fi

BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_NAME="${BRANCH_NAME##*/}"

BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
BRANCH_IN_COMMIT=$(grep -c "\[$BRANCH_NAME\]" $1)

if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]]; then 
  sed -i.bak -e "1s/^/[$BRANCH_NAME] /" $1
else
  sed -i.bak -e "1s/^/[NOTICKET] /" $1
fi

以上是关于sh 如何使用分支名称自动添加git commit的主要内容,如果未能解决你的问题,请参考以下文章

sh 准备提交消息git hook以预先添加具有分支名称的提交消息

如何使用分支名称显示 git log

git tag 重写

git提交分支代码发现写错分支名称,如何修改?

突然断电导致git分支错误解决办法

如何使用git回滚版本到分支中的某一个commit时间点