Git hook for macOS that generates commit messages from your staged diff using OpenAI's Responses API.
The hook is designed for local use: it keeps your API key out of the script, avoids logging diffs by default, and never blocks a commit if the AI request fails.
- Generates a commit message from
git diff --cached. - Uses OpenAI's current
/v1/responsesendpoint. - Uses a concise format: title plus bullet list.
- Reads the OpenAI key from
OPENAI_API_KEYor macOS Keychain. - Skips manual, merge, squash, and amend commits.
- Limits large diffs before sending them to OpenAI.
- Writes
Update changesas a fallback if AI generation fails and the commit message is empty. - Writes diagnostic logs to
prepare-commit-msg.lognext to the installed hook. - Does not log diff contents unless explicitly enabled.
- macOS.
- Xcode or Command Line Tools with Swift available through
xcrun swift. - Git 2.x or later.
- OpenAI API key.
Use a global Git hooks directory if you want the hook available in all local repositories.
-
Create a hooks directory:
mkdir -p ~/.ai-githooks -
Copy the hook:
cp prepare-commit-msg ~/.ai-githooks/prepare-commit-msg -
Make it executable:
chmod +x ~/.ai-githooks/prepare-commit-msg -
Configure Git to use that directory:
git config --global core.hooksPath ~/.ai-githooks -
Store your API key in macOS Keychain:
security add-generic-password \ -a "$USER" \ -s auto-commit-ai-openai-api-key \ -w "YOUR_OPENAI_API_KEY" \ -U
To disable the global hook later:
git config --global --unset core.hooksPathUse this if you only want the hook in one repository.
cp prepare-commit-msg /path/to/your/repo/.git/hooks/prepare-commit-msg
chmod +x /path/to/your/repo/.git/hooks/prepare-commit-msgThen configure the API key using either Keychain or OPENAI_API_KEY.
Recommended for normal local use:
security add-generic-password \
-a "$USER" \
-s auto-commit-ai-openai-api-key \
-w "YOUR_OPENAI_API_KEY" \
-UUseful for terminal-only workflows:
export OPENAI_API_KEY="YOUR_OPENAI_API_KEY"For GUI Git clients, environment variables may not be available. Keychain is usually more reliable on macOS.
All configuration is optional and uses environment variables.
| Variable | Default | Description |
|---|---|---|
OPENAI_API_KEY |
unset | OpenAI API key. Used before Keychain if present. |
AUTO_COMMIT_AI_MODEL |
gpt-5.4-mini |
OpenAI model. |
AUTO_COMMIT_AI_TIMEOUT |
30 |
Request timeout in seconds. |
AUTO_COMMIT_AI_MAX_DIFF_CHARS |
60000 |
Maximum diff characters sent to OpenAI. |
AUTO_COMMIT_AI_MAX_TOKENS |
1200 |
Maximum output tokens sent as max_output_tokens. |
AUTO_COMMIT_AI_FALLBACK_MESSAGE |
Update changes |
Message used if AI generation fails and the commit message is empty. |
AUTO_COMMIT_AI_KEYCHAIN_SERVICE |
auto-commit-ai-openai-api-key |
Keychain service name. |
AUTO_COMMIT_AI_LOG_DIFF |
unset | Set to true only if you want diff snippets in the log. |
-
Stage your changes:
git add . -
Commit without providing a message:
git commit
-
Review or edit the generated message in your Git editor.
The hook skips git commit -m "..." when the message is non-empty so manual messages are respected. Empty messages, including SourceTree's No message placeholder, receive an AI-generated message. It does not skip Git templates, because those are loaded before the editor opens.
The hook writes logs next to the installed script:
cat ~/.ai-githooks/prepare-commit-msg.logDiff contents are not logged by default. Enable AUTO_COMMIT_AI_LOG_DIFF=true only for temporary debugging.
The staged diff is sent to OpenAI. Do not use this hook for commits containing secrets or code that cannot leave your machine.
The hook limits large diffs, but it does not redact secrets automatically.
auto-commit-ai/
├── prepare-commit-msg
├── README.md
└── LICENSE
Arturo Carretero Calvo - 2026