LoginSignup
1
0

More than 1 year has passed since last update.

ブランチ毎にコミットメッセージのテンプレートを切り替えたい【Git】【prepare-commit-msg】

Posted at

はじめに

マスターブランチと、開発用の作業ブランチではコミットの粒度が異なるため、異なるテンプレートを用いたい。フックスクリプトを用いてテンプレートファイルを切り替えてみます。

前提

テンプレートファイル、およびその切り替えを行うフックスクリプトが、作業ディレクトリ内に以下のように配置されているものとします。

./work_directory
├── .git
│   ├── hooks
│   │   └── prepare-commit-msg
...省略
│
├── .templates
│   ├── develop_template.txt
│   └── master_template.txt
│
...省略

prepare-commit-msg

フックスクリプトの内容は、以下のようになります(実行権が必要です)。

prepare-commit-msg
#! /bin/bash

# 現在のブランチ名を取得する
branchName=$(git rev-parse --abbrev-ref HEAD)

template_content=""
if [[ "$branchName" == "master" ]]; then
    template_content=$(cat .templates/master_template.txt)
elif [[ "$branchName" == "develop" ]]; then
    template_content=$(cat .templates/develop_template.txt)
fi

mv $1 $1.tmp
echo "$template_content" > $1

おわりに

作業ディレクトリに対して設定をしました。global に設定したい場合は、パスを適切に変更してください。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0