Tired of writing Git commit messages? Let AI do the heavy lifting!
CommitAi streamlines your Git workflow by leveraging powerful AI models (OpenAI's GPT, Anthropic's Claude, Google's Gemini) to automatically generate clear, concise, and conventional commit messages based on your staged changes.
Simply stage your files and run commitai
. It analyzes the diff, optionally takes your high-level explanation, and crafts a commit message adhering to the Conventional Commits standard. Review the message in your default Git editor, make any tweaks, save, and you're done!
- ๐ง Intelligent Commit Generation: Analyzes staged code differences (
git diff --staged
) using state-of-the-art AI models (GPT, Claude, Gemini) to create meaningful commit messages. - ๐ Conventional Commits: Automatically formats messages according to the Conventional Commits specification (e.g.,
feat(auth): add JWT authentication
). This improves readability and enables automated changelog generation. - ๐ Optional Explanations: Provide a high-level description of your changes as input to guide the AI, or let it infer the context solely from the code diff.
- โ
Pre-commit Hook Integration: Automatically runs your existing native Git pre-commit hook (
.git/hooks/pre-commit
) before generating the message, ensuring code quality and style checks pass. - ๐ง Customizable Prompts via Templates: Add custom instructions or context to the AI prompt using global environment variables or repository-specific template files.
- ๐ค Multiple AI Provider Support: Choose your preferred AI model from OpenAI, Anthropic, Google or local AI models with Ollama.
- โ๏ธ Flexible Workflow:
- Stages all changes automatically (
-a
flag). - Reviews message in your default Git editor (default behavior).
- Commits directly without editor review (
-c
flag).
- Stages all changes automatically (
- โจ Modern Tooling: Built with
pyproject.toml
and formatted/linted withruff
.
(Demo shows generating a commit message using Claude 3 Opus without providing an explicit explanation)
Ensure you have Python 3.8+ and Git installed.
Install CommitAi directly from PyPI:
pip install commitai
CommitAi requires API keys for the AI provider you intend to use. Set these as environment variables:
-
OpenAI (GPT models):
export OPENAI_API_KEY="your_openai_api_key_here"
-
Anthropic (Claude models):
export ANTHROPIC_API_KEY="your_anthropic_api_key_here"
-
Google (Gemini models): Set one of the following (CommitAi checks in this priority order):
GOOGLE_API_KEY
(Recommended)GEMINI_API_KEY
GOOGLE_GENERATIVE_AI_API_KEY
Example:
export GOOGLE_API_KEY="your_google_api_key_here"
You only need to set the key for the provider corresponding to the model you select (or the default, Gemini).
CommitAi can also work with Ollama models:
export OLLAMA_HOST="your_ollama_base_url"
You can add custom instructions to the default system prompt used by the AI. This is useful for enforcing project-specific guidelines (e.g., mentioning ticket numbers).
-
Global Template: Set an environment variable. This applies to all repositories unless overridden locally.
# Example: Always ask the AI to reference a JIRA ticket format export TEMPLATE_COMMIT="Ensure the commit footer includes a JIRA reference like 'Refs: PROJECT-123'."
-
Repository-Specific Template: Use the
commitai-create-template
command within your repository. This creates a.git/commit_template.txt
file and overrides the globalTEMPLATE_COMMIT
variable for this repo only.# Example: Instruct the AI to focus on UI changes for this specific repo commitai-create-template "Focus the commit message body on user-facing UI changes."
Note: These templates add to the default system prompt, which already instructs the AI to follow Conventional Commits format. You are providing supplementary instructions.
- Make your code changes.
- Stage the changes you want to include in the commit:
git add <file1> <file2> ... # or stage all changes in tracked files git add . # or stage all changes including untracked files git add --all
- Run
commitai
:# Option 1: Let CommitAi infer the message from the diff commitai # Option 2: Provide a high-level explanation to guide the AI commitai "Refactor user authentication to use JWT tokens"
- Review & Edit: CommitAi runs pre-commit hooks, generates the message, and opens it in your default Git editor (e.g., Vim, Nano, VS Code). Review the message, make any necessary edits.
- Save & Close: Save the file and close the editor. CommitAi will then create the commit with the final message. If you close without saving or clear the message, the commit will be aborted.
The commitai
command (which is an alias for commitai generate
) accepts the following options:
-
-a
,--add
:- Automatically stages all unstaged changes (
git add --all
) before generating the commit message. - Useful for quickly committing everything in the working directory.
- Example:
commitai -a "Implement user profile page"
- Automatically stages all unstaged changes (
-
-c
,--commit
:- Skips opening the Git editor for review. The generated message is used to create the commit directly.
- Use with caution! Reviewing AI-generated messages is recommended.
- Example:
commitai -c "Fix typo in documentation"
(for minor changes) - Can be combined with
-a
:commitai -a -c "Quick fix and commit all"
-
-m <model_name>
,--model <model_name>
:- Specifies which AI model to use.
- Defaults to
gemini-2.5-pro-preview-03-25
. - Ensure the corresponding API key environment variable is set.
- Examples:
commitai -m gpt-4 "Use OpenAI's GPT-4"
commitai -m claude-3-opus-20240229 "Use Anthropic's Claude 3 Opus"
commitai -m gemini-2.5-flash-preview-04-17 "Use Google's Gemini 1.5 Flash"
The commitai-create-template
command sets a repository-specific template instruction.
commitai-create-template "Add a 'Co-authored-by:' line if applicable."
This creates/overwrites the .git/commit_template.txt
file in the current repository.
1. Simple commit, inferred message:
# Stage changes
git add src/utils.py tests/test_utils.py
# Run commitai - AI infers message from diff
commitai
(Editor opens with a message like feat(utils): add helper function for data validation
)
2. Commit with explanation:
# Stage changes
git add src/auth.py
# Run commitai with explanation
commitai "Implement password reset functionality using email tokens"
(Editor opens with a message like feat(auth): implement password reset via email token
)
3. Stage all and commit directly (no editor):
# Stage all changes and commit immediately using GPT-4
commitai -a -c -m gpt-4 "Minor refactoring and cleanup"
(Commit is created directly)
4. Using a Template:
- First, set a template:
commitai-create-template "Mention the related issue number from GitHub, e.g., Fixes #123."
- Then, run commitai:
git add src/parser.py commitai "Fix bug in CSV parsing logic"
(Editor opens with a message potentially like fix(parser): correct handling of quoted commas\n\nFixes #123
)
Contributions are highly welcome! Please follow these steps:
- Fork the repository on GitHub.
- Clone your fork locally:
git clone <your-fork-url>
- Navigate to the project directory:
cd commitai
- Create a virtual environment:
python -m venv .venv && source .venv/bin/activate
(or.\.venv\Scripts\activate
on Windows) - Install dependencies, including development tools:
pip install -e ".[test]"
- (Optional but Recommended) Set up pre-commit hooks:
pre-commit install
- Create a new branch for your feature or bug fix:
git checkout -b my-feature-branch
- Make your changes.
- Run checks locally before committing:
- Format code:
ruff format .
- Lint code:
ruff check .
- Run type checks:
mypy commitai tests
- Run tests:
pytest
- Format code:
- Commit your changes (you can use
commitai
!). - Push your branch to your fork:
git push origin my-feature-branch
- Open a pull request on the main
lguibr/commitai
repository.
The CI pipeline will automatically run all checks on your pull request.
CommitAi is open-source software licensed under the MIT License. See the LICENSE file for more details.