husky 工具
commit 前加上 branch name
使用 Husky 和一個 Git 釣子腳本來實現這個功能:以下是步驟:
- 安装 Husky:
npm install husky --save-dev
- 初始化 Husky:
npx husky install
- 創建一個 Git 釣子腳本來在 commit-msg 釣子上運行。首先,啟用 commit-msg 釣子:
npx husky add .husky/commit-msg 'scripts/commit-msg.sh'
- 創建
scripts/commit-msg.sh文件,並添加以下内容:
#!/bin/sh
BRANCH_NAME=$(git symbolic-ref --short HEAD)
COMMIT_MSG_FILE=$1
# 讀取現有的 commit message
COMMIT_MSG=$(cat $COMMIT_MSG_FILE)
# 在 commit message 前面加上 branch name
echo "[$BRANCH_NAME] $COMMIT_MSG" > $COMMIT_MSG_FILE
- 確保
scripts/commit-msg.sh文件是可執行的:
chmod +x scripts/commit-msg.sh
這樣,每次你提交代碼時,Husky 會自動在提交信息前面加上當前分支的名稱。