AIにGitコミットメッセージを考えてもらう (cz-gitのAI機能試してみた)
動機 & 流れ
「Gitのコミットメッセージを毎回考えるのめんどう・・・。」
「このご時世、CLIでも使えるツールを誰か作っているのでは?」
あった!!!!
よく見ると直近コミットはされてるけど、最新Relaseが2年前はメンテ止まってる・・・?
代わりにメンテされてて、Commitzenn的なツールはないのだろうか。。。
あった!!!
最新Releaseも数ヶ月以内でメンテナンスされていそう。
デモもいい感じ!!
早速試してみよう。
インストール
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発行
他のLLM Providerは以下が使えるよう。
- GitHub Models
- DeepSeek
- Ollama
OpenAIのAPI管理ページからトークン発行。
(※ 事前にクレジットカート登録が必要)
発行した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) を入力するとよいそう。
動作原理はこちら。
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など無料枠での活用を活かすのが良さそう。
参考
Discussion