コミットメッセージは直近10件のコミットを見てそのスタイルに合わせる。特になければConventional Commitを使う。
英語で書くの結構大変だし、自分のコミットメッセージより良くて勉強になる。
日本語とかで書かせたければプロンプト変えてくれ。
前提
Claude Code使える前提。
https://docs.anthropic.com/ja/docs/claude-code/sdk
APIセットするか、Claude Proプラン、Maxプランでも使える。
Gemini CLIとかに置き換えてもいいかもしれない。claude -p
のところを変えるだけなので。
設定の仕方
.gitconfig
のaliasの部分にこれ書くだけ。自分はcm
にしたけど、cmai
がAIのサジェスト
.gitconfig
[alias]
cm = "!f() { \
add_flag=''; \
while [ $# -gt 0 ]; do \
case $1 in \
-a|--all) add_flag='-u' ;; \
-A) add_flag='-A' ;; \
esac; \
shift; \
done; \
[ -n \"$add_flag\" ] && git add $add_flag; \
if [ -z \"$(git diff --staged)\" ]; then \
echo 'No staged changes'; \
return 1; \
fi; \
context=\"Recent commit messages in this project:\n\"; \
context+=$(git log --oneline -10 --pretty=format:'- %s'); \
context+=\"\n\nCurrent changes:\n\"; \
context+=$(git diff --staged); \
additional_prompt=''; \
while true; do \
if [ -n \"$additional_prompt\" ]; then \
prompt=\"$context\n\nAdditional instructions: $additional_prompt\"; \
else \
prompt=\"$context\"; \
fi; \
msg=$(echo \"$prompt\" | claude -p \
'Generate a commit message following the project style. If no clear pattern exists, use conventional commits. Output only commit message.'); \
echo \"Generated: $msg\"; \
read -p 'Use this message? (y/n/e/r=regenerate with prompt) ' choice; \
case $choice in \
y) git commit -m \"$msg\"; break ;; \
e) git commit -e -m \"$msg\"; break ;; \
n) echo 'Cancelled'; break ;; \
r) \
read -p 'Additional instructions (e.g., \"make it shorter\", \"add more detail\", \"mention the bug fix\"): ' additional_prompt; \
continue ;; \
*) echo 'Please choose y/n/e/r'; continue ;; \
esac; \
done; \
}; f"
使い方
想定しているコマンド
git cm
git cm -a # git addしてないのも含めてコミット
git cm -A # 新規ファイルも含めてコミット
生成されたメッセージが微妙だったら、編集やキャンセル、プロンプト入れて再生成できる。
Generated: feat: update API endpoints
Use this message? (y/n/e/r=regenerate with prompt) r
Additional instructions: v2 APIのことを書いて
Generated: feat: migrate endpoints to v2 API specification
使用例
git cm -a
Generated: feat: improve ai-commit alias prompt for better contextual commit messages
Use this message? (y/n/e/r=regenerate with prompt) r
Additional instructions (e.g., "make it shorter", "add more detail", "mention the bug fix"): use fix
Generated: fix: update ai-commit alias to use project-style commit messages
Use this message? (y/n/e/r=regenerate with prompt) y
[awakia/ai-commit 8a55ce8] fix: update ai-commit alias to use project-style commit messages