Skip to main content

Automatically reorganize messy git commit histories into clean, semantic commits

Project description

Git Smart Squash

An AI-powered tool that intelligently reorganizes your git commits into logical, reviewable chunks perfect for pull requests.

What It Does

Instead of manually squashing and organizing commits, Git Smart Squash:

  1. Analyzes your entire diff between your feature branch and the main branch
  2. Uses AI to organize changes into logical, reviewable commits
  3. Automatically restructures your git history to match the AI's recommendations
  4. Creates clean, conventional commits that are easy for reviewers to understand

Before vs After

Before (messy commits):

* 3a1b2c3 WIP: auth stuff and models
* 4d5e6f7 more changes
* 8g9h0i1 tests and docs

After (AI-organized commits):

* a1b2c3d feat: implement user authentication system
* e4f5g6h test: add comprehensive auth test coverage  
* i7j8k9l docs: update API documentation for auth endpoints

Installation

Install from PyPI (Recommended)

pip install git-smart-squash

Alternative Installation Methods

Using pipx (isolated environment):

pipx install git-smart-squash

From source:

git clone https://github.com/your-username/git-smart-squash.git
cd git-smart-squash
pip install -e .

Quick Start

1. Set up AI Provider

Local AI (Default - Recommended):

# Install and start Ollama
ollama serve
ollama pull devstral

Or use cloud AI:

# For OpenAI
export OPENAI_API_KEY="your-api-key"

# For Anthropic  
export ANTHROPIC_API_KEY="your-api-key"

2. Navigate to Your Git Repository

cd /path/to/your/repo
git checkout your-feature-branch

3. Run Git Smart Squash

Dry run (see what it would do):

git-smart-squash --dry-run

Apply the changes:

git-smart-squash

Use short command:

gss --dry-run  # Same as git-smart-squash --dry-run

Usage Examples

Basic Usage

# Preview the reorganization
git-smart-squash --dry-run

# Apply the reorganization  
git-smart-squash

# Use different base branch
git-smart-squash --base develop

# Use specific AI provider
git-smart-squash --ai-provider openai --model gpt-4.1

Advanced Usage

# Use OpenAI (automatically uses gpt-4.1)
git-smart-squash --ai-provider openai

# Use Anthropic (automatically uses claude-sonnet-4-20250514)
git-smart-squash --ai-provider anthropic

# Use specific model
git-smart-squash --ai-provider openai --model gpt-4.1

# Custom configuration
git-smart-squash --config .custom-config.yml

AI Providers

Local AI (Default)

  • Provider: Ollama with devstral model
  • Cost: Free
  • Privacy: Completely local
  • Setup: ollama serve && ollama pull devstral

OpenAI

  • Provider: OpenAI GPT models
  • Cost: Pay per use
  • Setup: Set OPENAI_API_KEY environment variable
  • Models: gpt-4.1

Anthropic

  • Provider: Anthropic Claude models
  • Cost: Pay per use
  • Setup: Set ANTHROPIC_API_KEY environment variable
  • Models: claude-sonnet-4-20250514

Configuration

Global Configuration

Create ~/.git-smart-squash.yml:

ai:
  provider: local  # or openai, anthropic
  model: devstral  # or gpt-4.1, claude-sonnet-4-20250514
  
output:
  backup_branch: true

Project Configuration

Create .git-smart-squash.yml in your project root:

ai:
  provider: openai
  model: gpt-4.1

Safety Features

Automatic Backups

  • Creates backup branch: your-branch-backup-1703123456
  • Preserves original work automatically
  • Easy recovery if needed

Recovery Commands

# If something goes wrong, recover your original branch:
git checkout your-branch-backup-1703123456
git checkout your-working-branch  
git reset --hard your-branch-backup-1703123456

Validation

  • Checks for clean working directory
  • Validates base branch exists
  • Confirms changes before applying

How It Works

The 4-Step Process

  1. Get Complete Diff: Uses git diff main...HEAD to capture all changes
  2. AI Analysis: Sends diff to AI with prompt for logical commit organization
  3. Display Plan: Shows proposed commit structure for review
  4. Apply Changes: Uses git reset --soft and creates new commits

Technical Details

  • Single Python file (~300 lines) for simplicity
  • Direct git commands via subprocess
  • Rich terminal UI for clear feedback
  • Conventional commit message standards
  • JSON response parsing with fallback mechanisms

Token Management (Local AI)

For large repositories, the tool automatically:

  • Estimates token usage (1 token ≈ 4 characters)
  • Sets optimal context size with hard cap at 12,000 tokens
  • Adjusts prediction limits up to 12,000 tokens
  • Scales timeouts for large requests (up to 600 seconds)

Troubleshooting

Common Issues

Ollama server not running:

ollama serve

Model not available:

ollama pull devstral

Large diffs timeout:

  • Break changes into smaller commits first
  • Use --base with more recent branch
  • Check token limit warnings

API key issues:

# Check environment variables
echo $OPENAI_API_KEY
echo $ANTHROPIC_API_KEY

Error Messages

"No changes found to reorganize":

  • Check you're on the right branch
  • Verify there are differences from base branch

"Failed to generate commit plan":

  • Check AI provider configuration
  • Verify network connectivity (for cloud providers)
  • Try with smaller diff

Examples

Simple Authentication Feature

Input diff:

+++ b/src/auth.py
+def authenticate(user, password):
+    return user == "admin"

+++ b/tests/test_auth.py  
+def test_authenticate():
+    assert authenticate("admin", "pass")

AI Output:

[
  {
    "message": "feat: add user authentication system",
    "files": ["src/auth.py", "tests/test_auth.py"], 
    "rationale": "Groups authentication implementation with its tests"
  }
]

Complex Multi-File Feature

AI organizes into logical commits:

  • feat: implement user authentication core
  • feat: add user model and database integration
  • test: add comprehensive auth test coverage
  • docs: update API documentation for auth endpoints

Contributing

Development Setup

git clone https://github.com/your-username/git-smart-squash.git
cd git-smart-squash
pip install -e .

# Run tests
python -m pytest test_functionality_comprehensive.py

# Run Ollama integration tests
./test_with_ollama.sh

Running Tests

Comprehensive functionality tests:

python test_functionality_comprehensive.py

Real Ollama integration tests:

python test_ollama_integration.py

License

MIT License - see LICENSE file for details.

Support

  • Issues: Report bugs and feature requests on GitHub
  • Documentation: See FUNCTIONALITY.md for detailed technical specs
  • AI Integration: See OLLAMA_INTEGRATION.md for token management details

Made with ❤️ for developers who want cleaner git history

Project details


Release history Release notifications | RSS feed

This version

2.3.5

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

git_smart_squash-2.3.5.tar.gz (16.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

git_smart_squash-2.3.5-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file git_smart_squash-2.3.5.tar.gz.

File metadata

  • Download URL: git_smart_squash-2.3.5.tar.gz
  • Upload date:
  • Size: 16.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for git_smart_squash-2.3.5.tar.gz
Algorithm Hash digest
SHA256 4bf1aed28147cb8c8fc58a7970e49ee35c28425a244d6377528ec4f61b85fc4b
MD5 e2c0c1d7fbfcee16d5a5f21f65c1a71b
BLAKE2b-256 a03e769d0594e2e8b8449a6b3239d4eaf8f595b9d47bbe1ddac952cafd86ec48

See more details on using hashes here.

File details

Details for the file git_smart_squash-2.3.5-py3-none-any.whl.

File metadata

File hashes

Hashes for git_smart_squash-2.3.5-py3-none-any.whl
Algorithm Hash digest
SHA256 9677ccd6a564a0747d6de7c425a69f090c2be119e6ed59d63cccffe0ea992d4f
MD5 12f58cb8f38a73a3499a7de047730055
BLAKE2b-256 b75c8b79912255813d350e495cb7ff2639d9d9d59b10a4f1b27f3d882bd8f7ff

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page