AI-powered commit message generator for git. Automatically generates conventional commit messages based on your staged changes.
- 🤖 AI-powered: Uses AI to analyze your code changes and generate meaningful commit messages
- 📝 Conventional Commits: Follows the Conventional Commits format
- 🔧 Easy Configuration: Simple
.envsetup with environment variable support - 🛡️ Safe: Doesn't block commit on errors - falls back gracefully
- 🔌 OpenAI Compatible: Works with OpenAI, Azure OpenAI, and any OpenAI-compatible API
- ⚡ CLI Ready: Can be used directly with
pnpm execor installed globally
# With npm
npm install --save-dev @light-cat/ai-commit-msg
# With pnpm
pnpm add --save-dev @light-cat/ai-commit-msg
# Set up git hooks (installs simple-git-hooks if needed)
pnpm exec ai-commit setup# With npm
npm install -g @light-cat/ai-commit-msg
# With pnpm
pnpm add -g @light-cat/ai-commit-msg
# Then set up git hooks in your project
cd /path/to/your/project
pnpm exec ai-commit setupCreate a .env file in your project root or set environment variables:
# Required
AI_API_KEY=your-api-key-here
# Optional (defaults shown)
AI_API_BASE_URL=https://api.openai.com/v1
AI_MODEL=gpt-4| Provider | AI_API_BASE_URL |
AI_MODEL |
|---|---|---|
| OpenAI | https://api.openai.com/v1 |
gpt-4, gpt-3.5-turbo |
| Azure OpenAI | Your Azure endpoint | Your deployment name |
| Local/Other | Your custom endpoint | Any OpenAI-compatible model |
- OpenAI: Get your API key from platform.openai.com
After setting up the git hook, simply commit your changes without providing a message:
# Stage your changes
git add .
# Commit - AI will automatically generate a commit message
git commitThe tool will:
- Fetch staged changes
- Send them to the AI service
- Generate a conventional commit message
- Write it to the commit message file
# Install hooks and configure simple-git-hooks automatically
pnpm exec ai-commit setupAdd to your package.json:
{
"simple-git-hooks": {
"prepare-commit-msg": "pnpm exec ai-commit"
}
}Or if using lefthook:
# lefthook.yml
pre-commit:
commands:
generate-commit-msg:
run: pnpm exec ai-commit# Dry run (show message without writing)
pnpm exec ai-commit --dry-run
# Verbose mode (show debug info)
pnpm exec ai-commit --verbose
# Help
pnpm exec ai-commit --help- When you run
git commit, theprepare-commit-msghook is triggered - The tool fetches:
- Staged changes (
git diff --cached) - Current branch name
- Last commit message (if available)
- Staged changes (
- Sends this information to the AI service
- Generates a conventional commit message
- Writes it to the commit message file
Given this staged change:
diff --git a/src/auth.ts b/src/auth.ts
+export function validateEmail(email: string): boolean {
+ return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
+}The tool might generate:
feat(auth): add email validation function
Make sure your .env file exists and contains AI_API_KEY, or set the environment variable before committing.
- Check if changes are staged (
git addfirst) - Run with
--verboseto see debug information - Ensure your API key has sufficient credits/permissions
- Verify git hooks are installed:
cat .git/hooks/prepare-commit-msg
Simply provide a commit message when running git commit -m "your message" - the tool will detect an existing message and skip generation.
Contributions are welcome! Please feel free to submit a Pull Request.
Run the source code directly with tsx (recommended for ESM projects):
cd /path/to/node-ai-commit
pnpm tsx bin/ai-commit.js --helpOr using the dev script:
pnpm dev -- --helpNote:
ts-nodehas limited ESM support. Usetsxinstead for better compatibility.
Link the local package globally and test in other projects:
cd /path/to/node-ai-commit
pnpm link
# In another project
cd /path/to/your-project
pnpm link @light-cat/ai-commit-msg
pnpm exec ai-commit setupIn another project's package.json:
{
"devDependencies": {
"@light-cat/ai-commit-msg": "link:/path/to/node-ai-commit"
}
}Then run pnpm install or npm install in that project.
# Build the TypeScript
pnpm build
# Run locally (requires build first)
node bin/ai-commit.js --help
# Or run directly without building
pnpm tsx bin/ai-commit.js --help- Create a test repository:
mkdir test-ai-commit && cd test-ai-commit
git init- Link the local package:
cd /path/to/node-ai-commit
pnpm link --global
cd /path/to/test-ai-commit
pnpm link --global @light-cat/ai-commit-msg- Set up and test:
# Create .env with your API key
pnpm exec ai-commit setup
# Make some changes and commit (don't provide message, let AI generate it)
git add .
git commit