Fract-Blog
Tips

GitHub Copilot のコミットメッセージ生成をカスタマイズする

GitHub Copilot を使ってコミットメッセージを生成する際に、特定のフォーマット・内容・日本語などを指定する方法を紹介します。

GitHub Copilot のコミットメッセージ生成を指示する

会社でGitHub Copilotなどを使用していないためポートフォリオ用のプロジェクトで本格的に使用してみたところ、メッセージが英語で生成されてしまい、 コミットメッセージのフォーマットも整っていなかったため、GitHub Copilot にコミットメッセージの生成を指示する方法を調べてみました。

指定方法

vscodeではsettings.jsonに以下のように記述することで、GitHub Copilot にコミットメッセージの生成を指示できます。

{
    "github.copilot.chat.commitMessageGeneration.instructions": [
        {
            "text": "文章での指示"
        },
        {
            "file": "指示が書かれているファイルのパス"
        },
       // ... 上記を複数指定可能
    ]
}

実際に生成されるコミットメッセージの例

現在作成中のポートフォリオ用のプロジェクトでCleanArchitecture用のディレクトリを作成した際に、以下のようなコミットメッセージが生成されました。 これは上記の設定を適用する前のものです。

feat: add .gitkeep files to maintain directory
structure

今回設定したルールは以下になります。 こちらの2つ目のルールはokineadevさんのGistに掲載されています。

{
    "github.copilot.chat.commitMessageGeneration.instructions": [
        {
            "text": "Make sure you write your commit message in Japanese."
        },
        {
            "text": "Follow the Conventional Commits format strictly for commit messages. Use the structure below:\n\n```\n<type>[optional scope]: <gitmoji> <description>\n\n[optional body]\n```\n\nGuidelines:\n\n1. **Type and Scope**: Choose an appropriate type (e.g., `feat`, `fix`) and optional scope to describe the affected module or feature.\n\n2. **Gitmoji**: Include a relevant `gitmoji` that best represents the nature of the change.\n\n3. **Description**: Write a concise, informative description in the header; use backticks if referencing code or specific terms.\n\n4. **Body**: For additional details, use a well-structured body section:\n   - Use bullet points (`*`) for clarity.\n   - Clearly describe the motivation, context, or technical details behind the change, if applicable.\n\nCommit messages should be clear, informative, and professional, aiding readability and project tracking."
        },
    ]
}

そして生成されたコミットメッセージは以下のようになります。

feat: ✨ CleanArchitectureのフォルダ構成を作成
* `api`, `controllers`, `domain`, `infrastructure`, `presenters`, `usecases` などの新しいディレクトリに.gitkeepファイルを追加しました。
* 空のディレクトリを保持するために必要です。

しっかりとルールに従って、日本語化+Gitmoji+Conventional Commitsのフォーマットに従ったコミットメッセージが生成されました。

Conventional Commitsとは?

私は今まで知らなかったのですが、Conventional Commitsはコミットメッセージのための軽量な規約であるらしいです。 この規則に従いコミットメッセージのテンプレートを生成してくれるツールもあるようです。

Conventional Commitの仕様書をCopilotに指示する

今回は./.docs/conventional-commits.mdというファイルを作成し、以下の内容を記述しました。

{
    "github.copilot.chat.commitMessageGeneration.instructions": [
        {
            "file": ".docs/conventional-commits.md"
        },
    ]
}
conventional-commits.mdの内容

Conventional Commits Usage Guide

📘 This document includes material from the Conventional Commits v1.0.0 Specification, licensed under Creative Commons Attribution 3.0 International (CC BY 3.0).
Copyright © The Conventional Commits Specification Authors.


✍️ Commit Message Format


<type>(<scope>)\<optional !>: <description>

\[optional body]

\[optional footer(s)]
  • The message must start with a type (feat, fix, docs, etc.).
  • A scope may be included in parentheses.
  • If the commit includes a breaking change, ! must be added before the colon.
  • A description must follow after a colon and a space.

✅ Examples

Basic Feature and Fix

feat(login): add support for OAuth2

fix(parser): handle null inputs correctly

With Body

feat(profile): allow avatar image uploads

This adds support for uploading avatar images using multipart/form-data.
Thumbnails are generated automatically using sharp.
fix(auth): reject expired tokens

Tokens are now checked for expiration during validation.

Closes: #42

Breaking Change (in header)

refactor(api)!: drop support for legacy v1 endpoints

BREAKING CHANGE: API clients must now use the v2 endpoints.
chore(deps): update express to v5

BREAKING CHANGE: Express 5 no longer supports middleware chaining via `next('route')`.

📌 Common type Values

TypeMeaning
featA new feature
fixA bug fix
docsDocumentation only changes
styleCode style (formatting, etc.)
refactorCode refactoring
perfPerformance improvement
testAdding or fixing tests
choreBuild process or tool changes

You may define and use additional types if needed.


🔗 References

上記の内容を指示することで、よりCopilotはConventional Commitsのフォーマットに従ったコミットメッセージを生成することができます

feat(docs): ✨ ER図とセッション管理フロー図を更新
- ER図に新しいテーブルと関係を追加
- 不要なファイルを削除
- セッション管理フロー図のタイトルを修正

先ほどのこのコミットメッセージよりも<docs>などをしっかりと指定してくれています。

<docs>のdocsフォルダ内部のファイルを変更したので指定してくれているのだと思います。

まとめ

今回はGitHub Copilot を使ってコミットメッセージを生成する際に、特定のフォーマット・内容・日本語などを指定する方法を紹介しました。 意外と頭の中ではどんな作業を行ったのかを把握していても、いざメッセージを書くとなると意外と筆(タイピング)が進まないことがありませんか?私はそうでした。 とりあえず生成できること書き始める一歩目がとても楽になりました。

今回は厳しくしていませんが以下のようなlinterを導入することで、より良いコミットメッセージを生成できるようになると思います

参考

https://zenn.dev/prog_hallelujah/articles/b57ddf2c6763ce https://gist.github.com/okineadev/8a3f392a93ae50e8d523e4ba7f9f9ac3 https://www.conventionalcommits.org/ja/v1.0.0/