A Git hook for macOS that automatically generates detailed, well-structured commit messages using the OpenAI API (model gpt-4o-mini
). Perfect for maintaining a clear and consistent commit history without manual effort.
- Automatic message generation based on the staged diff (
git diff --cached
). - Structured output: paragraphs, bullet points, or line breaks for multiple changes.
- Up to 2500 characters to cover all relevant modifications.
- Detailed logging in
prepare-commit-msg.log
within the hooks directory. - Compatible with both the command line and Git GUI clients (e.g., SourceTree: create commit with empty text).
- macOS with Swift installed (Xcode or Command Line Tools).
- Git 2.x or later.
- OpenAI API key.
Optional: To apply this hook to all your Git repositories without copying it manually into each .git/hooks folder, follow these steps:
- **Create a directory for global hooks:
mkdir -p ~/.ai-githooks
- **Copy the hook script into that directory:
cp auto-commit-ai/prepare-commit-msg ~/.ai-githooks/prepare-commit-msg
- **Make it executable:
chmod +x ~/.ai-githooks/prepare-commit-msg
- **Configure Git to use your global hooks directory:
git config --global core.hooksPath ~/.ai-githooks
- **Insert your OpenAI API key in the script (only once inside ~/.ai-githooks/prepare-commit-msg):
let finalKey = "YOUR_API_KEY_HERE"
Optional: To revert to the default Git hooks behavior:
git config --global --unset core.hooksPath
-
Clone this repository into your project:
git clone https://github.com/your-username/auto-commit-ai.git
[email protected]:ArtCC/auto-commit-ai.git
-
Copy the hook into your repo’s hooks folder:
cp auto-commit-ai/prepare-commit-msg .git/hooks/prepare-commit-msg
rm -rf auto-commit-ai
-
Make it executable:
chmod +x .git/hooks/prepare-commit-msg
-
Insert your OpenAI API key in the script:
// Inside .git/hooks/prepare-commit-msg let finalKey = "YOUR_API_KEY_HERE"
- Change the shebang to your Swift interpreter if needed:
#!/usr/bin/env swift
- Adjust the timeout by modifying the
sem.wait(timeout: .now() + 15)
value. - Switch to a different OpenAI model by updating
model: "gpt-4o-mini"
.
- Make your code changes.
- Stage them:
git add .
- Commit as usual:
git commit
- The hook will send the staged diff to OpenAI and prepend the generated message to
COMMIT_EDITMSG
. - Inspect the log for details or errors on installation folder:
cat prepare-commit-msg.log
auto-commit-ai/
├── prepare-commit-msg # Swift hook script
└── README.md # Project documentation
└── LICENSE # Project license
Contributions are welcome! Please:
- Open issues to report bugs or suggest enhancements.
- Submit pull requests with new features or fixes.
Ensure your code follows the established Swift style and include tests or examples where appropriate.
Arturo Carretero Calvo - 2025