6

将Git的分支名称添加到提交消息中

 1 year ago
source link: https://yangjiahao106.github.io/2022/07/27/%E5%B0%86Git%E7%9A%84%E5%88%86%E6%94%AF%E5%90%8D%E7%A7%B0%E6%B7%BB%E5%8A%A0%E5%88%B0%E6%8F%90%E4%BA%A4%E6%B6%88%E6%81%AF%E4%B8%AD/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

将Git的分支名称添加到提交消息中

  • 在 .git/hooks 目录下创建 prepare-commit-msg文件,并将下面的脚本放入文件中,
#!/bin/bash

# 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)
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
fi
  • 为文件添加可执行权限
chmod 755 prepare-commit-msg

完成之后分支名就会出现在提交信息里了, 例如:

Date:   Mon Oct 10 15:00:47 2022 +0800

    [feature/cms]  fix bug

Share


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK