準備
prepare-commit-msgフック
を使う。
prepare-commit-msgの引数は3つで、1つめがコミットメッセージ用ファイル。
.git/hooks/prepare-commit-msg
#!/bin/sh
# ブランチ名取得
branch=$(git branch | grep ^* | sed 's/\* //g')
# first commit前等で、ブランチ名が取れなかった時用
if [ ! $branch ]; then branch='master'; fi
# コミットメッセージの置換
msg=$(cat $1 | sed -e "s/ (branch:[0-9a-zA-Z_]\+)$//g")
if [ $branch != 'master' ]; then
msg=$msg" (branch:"$branch")"
fi
echo $msg > $1
実行権限を忘れずに
$ chmod +x .git/hooks/prepare-commit-msg
実行例
$ mkdir -p /path/to/test_dir
$ cd /path/to/test_dir
$
$ git init
Initialized empty Git repository in /path/to/test_dir/.git/
(master #)$
(master #)$ vi .git/hooks/prepare-commit-msg
(master #)$
(master #)$ chmod +x .git/hooks/prepare-commit-msg
(master #)$
(master #)$ touch REDME
(master #)$
(master #)$ git add .
(master #)$
(master #)$ git commit -m 'first commit'
[master (root-commit) 6b358fa] first commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 REDME
(master)$
(master)$ touch test00
(master)$ git add .
(master +)$ git commit -m 'file add'
[master 45b335f] file add
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test00
(master)$
(master)$ git log --oneline
45b335f file add
6b358fa first commit
(master)$
(master)$ git co -b test01
Switched to a new branch 'test01'
(test01)$
(test01)$ touch test01
(test01)$ git add .
(test01 +)$ git commit -m 'file add'
[test01 2bc512f] file add (branch:test01)
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test01
(test01)$
(test01)$ git log --oneline
2bc512f file add (branch:test01)
45b335f file add
6b358fa first commit
(test01)$
(test01)$ git checkout master
Switched to branch 'master'
(master)$
(master)$ git cherry-pick 2bc512f
[master dee0aab] file add
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test01
(master)$
(master)$ git log --oneline
dee0aab file add
45b335f file add
6b358fa first commit
(master)$
(master)$ git reset --hard HEAD^
HEAD is now at 45b335f file add
(master)$
(master)$ git co -b test02
Switched to a new branch 'test02'
(test02)$
(test02)$ git log --oneline
45b335f file add
6b358fa first commit
(test02)$
(test02)$ git cherry-pick 2bc512f
[test02 b781690] file add (branch:test02)
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test01
(test02)$
(test02)$ git log --oneline
b781690 file add (branch:test02)
45b335f file add
6b358fa first commit
※__git_ps1
で、プロンプトにブランチ名を表示しています
Comments