✍️

AIにGitコミットメッセージを考えてもらう (cz-gitのAI機能試してみた)

に公開

動機 & 流れ

「Gitのコミットメッセージを毎回考えるのめんどう・・・。」
「このご時世、CLIでも使えるツールを誰か作っているのでは?」

https://github.com/di-sukharev/opencommit
あった!!!!

よく見ると直近コミットはされてるけど、最新Relaseが2年前はメンテ止まってる・・・?

代わりにメンテされてて、Commitzenn的なツールはないのだろうか。。。

https://github.com/Zhengqbbb/cz-git
あった!!!

最新Releaseも数ヶ月以内でメンテナンスされていそう。

デモもいい感じ!!

https://cz-git.qbb.sh/cli/ai

早速試してみよう。

インストール

https://cz-git.qbb.sh/guide/#as-global-use

npm packageとして開発されているが、npmじゃなくてもいけるよう。

brew install czg

コンフィグファイル作成 (バージョン含むの面倒だな。。。)

echo '{ "path": "cz-git", "$schema": "https://cdn.jsdelivr.net/gh/Zhengqbbb/cz-git@1.11.1/docs/public/schema/cz-git.json" }' > ~/.czrc

OpenAI API Key発行

https://cz-git.qbb.sh/recipes/openai

他のLLM Providerは以下が使えるよう。

  • GitHub Models
  • DeepSeek
  • Ollama

OpenAIのAPI管理ページからトークン発行。

https://platform.openai.com/settings/organization/api-keys

(※ 事前にクレジットカート登録が必要)

発行したAPI Keyをコピー。

セットアップ

コピーしたAPI Keyを--api-keyの後に貼り付け.

czg --api-key=sk-xxxxx

実行例

$ czg --api-key=xxx
>>> Setup OpenAI API key on /Users/usename/.config/.czrc successfully

試す

準備

$ mkdir -p /tmp/sample-repo
$ cd /tmp/sample-repo
$ git init
$ cat << EOF > sample.txt
Hello world!
EOF
$ git add sample.txt

実行

$ czg ai

選択肢が出てくる。

? Select the type of change that you're committing: Use arrow keys or type to search
❯ feat:     A new feature
  fix:      A bug fix
  docs:     Documentation only changes
  style:    Changes that do not affect the meaning of the code
  refactor: A code change that neither fixes a bug nor adds a feature
  perf:     A code change that improves performance
  test:     Adding missing tests or correcting existing tests

作成されたコミットメッセージが表示される。あまりに自然言語だけどまぁヨシ。

? Select the type of change that you're committing: feat:     A new feature
ℹ Generating your AI commit subject...

###--------------------------------------------------------###
feat: add a new file with a greeting message "Hello world!
###--------------------------------------------------------###

? Are you sure you want to proceed with the commit above? (Ynemh)

y → Enter でコミット作成。

? Are you sure you want to proceed with the commit above? Yes
[main (root-commit) 409a7fb] feat: add a new file with a greeting message "Hello world!
 1 file changed, 1 insertion(+)
 create mode 100644 sample.txt

作成されてる。

$ git show
commit 409a7fb0e26d3a1b26496e6da81f0e1f65efa2cb (HEAD -> main)
Author: daylight55 <jojomasanori3@gmail.com>
Date:   Fri Jun 6 23:58:41 2025 +0900

    feat: add a new file with a greeting message "Hello world!

ひとまず、Conventional Commitを選択してコミット出来るから使えなくもなさそう。

料金確認

使用されたトークン量

検証含めて、2回問い合わせしているから、1回あたり In:99 Out: 12 トークン?

この量ならまだ切り捨て0$。

参考: https://gptforwork.com/tools/openai-chatgpt-api-pricing-calculator

補足

コミットメッセージの編集をしたいときは、メッセージ表示のプロンプトで m(Modify) を入力するとよいそう。
https://cz-git.qbb.sh/recipes/openai#how-to-modify-message-after-ai-generation

動作原理はこちら。
https://cz-git.qbb.sh/recipes/openai#how-it-works

Run git diff to obtain difference code information, combine prompt task, send them to OpenAI API - /chat/completions, Return the subjects information generated by AI.

git diffの結果を送って、OpanAI APIの/chat/completionsでコミットメッセージを生成しているよう。

感想

  • コミットメッセージが開発用語的ではないため、本格利用にはチューニングが必要そう。
  • diffが大きいとお金がかかるため、GitHub Modelsなど無料枠での活用を活かすのが良さそう。

参考

https://zenn.dev/mozumasu/articles/mozumasu-cz-git

Discussion