はじめに
コミットメッセージのテンプレートを使って、Conventional Commitsを実践してみたいと思います。
Conventional Commitsとは
Conventional Commitsは、コミットメッセージの書き方のルールです。
Conventional Commits
人間と機械が読みやすく、意味のあるコミットメッセージにするための仕様
フォーマット
Conventional Commitsのフォーマットは以下の通りです。
1<type>[optional scope]: <description>
2
3[optional body]
4
5[optional footer(s)]例えば、
1fix: fix bug
2
3Ref #131feat: add new feature
2
3BREAKING CHANGE: add new featureみたいになります。
Type
<type>は以下のようなものがあります。
feat: 新機能fix: バグ修正docs: ドキュメントの変更style: コードの意味に影響を与えない変更(空白、フォーマット、セミコロンの欠落など)refactor: 既存の機能の変更perf: パフォーマンスの向上test: テストの追加、変更build: ビルドシステムや外部依存関係の変更ci: CIの設定やスクリプトの変更chore: その他の変更revert: リバート
Semantic Versioning
Conventional Commitsを使うことで、Semantic Versioning(MAJOR.MINOR.PATCH)でコミットをまとめることができます。
MAJOR、MINOR、PATCHは以下のように決まります。
MAJOR: フッターにBREEAKING CHANGEが含まれる、もしくは<type>[optional scope]の直後に!が含まれるMINOR:<type>がfeatPATCH:<type>がfix
メリット
Convertional Commitsを使うことで、以下のようなメリットがあります。
- CHANGELOGを自動生成できる(conventional-changelog)
- フォーマットが統一されることで、コミットメッセージが読みやすくなる
<type>を使って、ビルドやリリースの自動化がしやすくなる- Semantic Versioningでコミットをまとめられる
(補足) Semantic Commit Messages
関連した書き方のルールとして、Semantic Commit Messagesもあります。

Semantic Commit Messages
Semantic Commit Messages. GitHub Gist: instantly share code, notes, and snippets.
commit template
Gitでは、コミットメッセージのテンプレートを使うことができます。
テンプレートの作成
まずは、下記のようなコミットのテンプレートとなるファイル.gitmessage.txtを作成します。
1# This is comment
2<subject>
3
4<body>
5
6[issue: <issue number>]
7# This is commentテンプレートの設定
作成したファイルをgit configで、コミットのテンプレートとして設定します。
グローバルな設定であれば、以下のように設定します。
1git config --global commit.template <PATH_TO>/.gitmessage.txtローカルな設定であれば、以下のように設定します。
1git config --local commit.template <PATH_TO>/.gitmessage.txtテンプレートの利用
テンプレートが設定されていると、git commitを実行した際に、下記のようにテンプレートが表示されます。
1# This is comment
2<subject>
3
4<body>
5
6[issue: <issue number>]
7# This is comment
8# Please enter the commit message for your changes. Lines starting
9# with '#' will be ignored, and an empty message aborts the commit.
10#
11# On branch main
12# Your branch is up to date with 'origin/main'.
13#
14# Changes to be committed:
15# new file: sample.txt
16#
17# Untracked files:
18# .gitmessage.txt
19#こちらを編集して、コミットメッセージを入力します。#で始まる行はコメントとして無視されます。
Conventional Commits用のcommit template
ということで、Conventional Commits用のcommit templateを作成してみました。gitmojiから、それぞれのTypeに対応する絵文字も使っています。
1#
2#########################
3# Start of Commit Message
4#########################
5#
6#
7# <type>[optional scope]: <description>
8# ✨ feat:
9# 🐛 fix:
10# 📝 docs:
11# 🎨 style:
12# ♻️ refactor:
13# ⚡️ perf:
14# ✅ test:
15# 👷 chore:
16
17# [optional body]
18
19# [optional footer(s)]
20# Ref:
21
22#
23#
24#######################
25# End of Commit Message
26#######################
27#
28#
29# ===== Type(emoji) =====
30# feat: A new feature
31# fix: A bug fix
32# docs: Documentation only changes
33# style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
34# refactor: A code change that neither fixes a bug nor adds a feature
35# perf: A code change that improves performance
36# test: Adding missing or correcting existing tests
37# chore: Changes to the build process or auxiliary tools and libraries such as documentation generation
38#
39# ===== Example =====
40# 🐛 fix: prevent racing of requests
41#
42# Introduce a request id and a reference to latest request. Dismiss
43# incoming responses other than from latest request.
44#
45# Remove timeouts which were used to mitigate the racing issue but are
46# obsolete now.
47#
48# Reviewed-by: Z
49# Ref: #123
50参考
- Conventional Commits
- Semantic Commit Messages
- Git - Git の設定
- How to Take Your Git Commit Messages to The Next Level With a Commit Template | by Efren Marin | Medium
- gitmoji | An emoji guide for your commit messages
- conventional-changelog/conventional-changelog: Generate changelogs and release notes from a project's commit messages and metadata.




