Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
15
Help us understand the problem. What is going on with this article?
@ie4

コミットメッセージ末尾にブランチ名を付加

More than 5 years have passed since last update.

準備

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で、プロンプトにブランチ名を表示しています

参考URL

Git フック

15
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ie4
IoTっぽい記事多めです。
iotlt
IoT縛りの勉強会です。 毎月イベントを実施しているので是非遊びに来てください! 登壇者を中心にQiitaでも情報発信していきます。 https://iotlt.connpass.com

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
15
Help us understand the problem. What is going on with this article?