auto_commit.py
is a Python script that automates the process of generating concise, conventional commit messages for your Git repository changes using an LLM (via Ollama). It can commit all changes at once or commit each file separately, with AI-generated commit messages based on the actual diffs.

- AI-generated commit messages: Uses an LLM to analyze Git diffs and suggest relevant, conventional commit messages (e.g., feat:, fix:, chore:).
- Commit all or per file: Optionally commits all changes together or each file separately, each with its own message.
- Handles new, modified, and deleted files.
- Works with both staged and unstaged changes.
- Detects changed files (staged and unstaged).
- Gets diffs for each file.
- Sends diffs and file info to the LLM via Ollama to generate a commit message.
- Commits changes using the generated message(s).
flowchart TD
A[Start] --> B[Parse Arguments]
B --> C{Get Changed Files}
C -->|No changes| Z[No changes detected.]
C -->|Changes found| D{Commit Per File?}
D -->|Yes| E[For each file:<br>1. Get diff<br>2. Generate commit message<br>3. Commit file]
D -->|No| F[For each file:<br>1. Get diff<br>2. Generate commit message]
F --> G[Combine all messages]
G --> H[Commit all changes with combined message]
E --> I[Done]
H --> I
Z --> I
I[End]
-
Python 3.7+
This script currently uses the gemma3:4b
model.
ollama run gemma3:4b
pip install ollama
- Git installed and available in PATH
If ollama server is running, run the script using the command
python auto_commit.py <repository_path> [single]
- <repository_path>: Path to your Git repository.
- single (optional): If provided, commits each file separately; otherwise, all changes are committed together.
Commit all changes in git:
python3 auto_commit.py /Users/ttpho/Documents/GitHub/chat
Commit each file separately:
python3 auto_commit.py /Users/ttpho/Documents/GitHub/chat single
- The script uses the
gemma3:4b
model by default. You can change the model by editing the model variable. - Commit messages are limited to 72 characters and follow the Conventional Commits style.
- The script will print the generated commit messages before committing.
Contributions and ideas are welcome, such as:
-
Modifying the prompt, model, or LLM provider.
-
Generating commit messages for individual files or a single message for all files.
-
Optimize or refactor...