Skip to content

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.

License

Notifications You must be signed in to change notification settings

ttpho/git_diff_auto_commit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Overview

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.

Features

  • 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.

How It Works

  1. Detects changed files (staged and unstaged).
  2. Gets diffs for each file.
  3. Sends diffs and file info to the LLM via Ollama to generate a commit message.
  4. 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]
Loading

Installation

This script currently uses the gemma3:4b model.

ollama run gemma3:4b
pip install ollama
  • Git installed and available in PATH

Usage

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.

Example

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

Notes

  • 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.

Miscellaneous

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...

About

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.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages