Skip to content

Git Commit with AI Generated Messages

lewistransts edited this page Dec 8, 2025 · 3 revisions

Overview

Automate the creation of high-quality, standardized commit messages using AgentCrew AI. This recipe demonstrates how to leverage AI to generate Conventional Commits compliant messages from your staged git changes.

Benefits

  • Consistency: All commit messages follow the same professional format
  • Time Savings: Eliminate the mental overhead of crafting commit messages
  • Best Practices: Automatically adhere to Conventional Commits specification
  • Code Review: Clearer commit history improves team collaboration

Prerequisites

Before implementing this recipe, ensure you have:

  • Git installed and configured
  • AgentCrew installed (Installation Guide)
  • ✅ Working in a git repository with staged changes
  • ✅ API access to an LLM provider (e.g., OpenAI)

How It Works

Workflow

1. Stage your changes          → git add <files>
2. Run the AI commit function  → ai_commit
3. AI analyzes staged diff     → Generates commit message
4. Auto-commit with message    → Changes committed

Architecture

The solution consists of two components:

  1. CommitMessageGenerator Agent: An AgentCrew agent configured with strict prompt rules to generate Conventional Commits
  2. Shell Function: A wrapper script that orchestrates the git diff → AI generation → commit workflow

Configuration

Choose the implementation for your shell environment:

Bash (Linux/macOS)

Add this function to your ~/.bashrc or ~/.zshrc:

ai_commit() {
  # Check dependencies and git repo
  command -v agentcrew >/dev/null || { echo "Error: agentcrew not found" >&2; return 1; }
  command -v git >/dev/null || { echo "Error: git not found" >&2; return 1; }
  git rev-parse --git-dir >/dev/null 2>&1 || { echo "Error: Not in git repo" >&2; return 1; }

  # Get staged changes
  local diff_content
  diff_content=$(git diff --staged)
  [[ -z "$diff_content" ]] && { echo "Error: No staged changes. Use 'git add' first" >&2; return 1; }

  # Generate commit message
  echo "Generating commit message..."
  local message
  message=$(agentcrew job \
    --agent="CommitMessageGenerator" \
    --agent-config='https://raw.githubusercontent.com/saigontechnology/AgentCrew/refs/heads/main/examples/agents/jobs/commit-message-generator.toml' \
    --provider=openai \
    --model-id="gpt-4o-mini" \
    "$diff_content" 2>&1)

  [[ $? -ne 0 || -z "$message" ]] && { echo "Error: Failed to generate message" >&2; return 1; }

  # Clean and display message
  message=$(echo "$message" | sed 's/^["'\''[:space:]]*//;s/["'\''[:space:]]*$//')
  echo -e "\nGenerated message:\n$message\n"

  # Commit changes
  git commit -m "$message" && echo "Successfully committed!"
}

To activate: Run source ~/.bashrc or restart your terminal


PowerShell (Windows)

Add this function to your PowerShell profile ($PROFILE):

function Invoke-AICommit {
    # Check dependencies
    if (-not (Get-Command agentcrew -ErrorAction SilentlyContinue)) {
        Write-Error "Error: agentcrew not found"
        return
    }
    if (-not (Get-Command git -ErrorAction SilentlyContinue)) {
        Write-Error "Error: git not found"
        return
    }

    # Check git repo and staged changes
    $null = git rev-parse --git-dir 2>&1
    if ($LASTEXITCODE -ne 0) {
        Write-Error "Error: Not in git repository"
        return
    }

    $diffContent = git diff --staged
    if ([string]::IsNullOrWhiteSpace($diffContent)) {
        Write-Error "Error: No staged changes. Use 'git add' first"
        return
    }

    # Generate commit message
    Write-Host "Generating commit message..." -ForegroundColor Cyan

    $message = agentcrew job `
        --agent="CommitMessageGenerator" `
        --agent-config='https://raw.githubusercontent.com/saigontechnology/AgentCrew/refs/heads/main/examples/agents/jobs/commit-message-generator.toml' `
        --provider='openai' `
        --model-id="gpt-4o-mini" `
        "$diffContent" 2>&1

    if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($message)) {
        Write-Error "Error: Failed to generate commit message"
        return
    }

    # Clean and display message
    $message = $message.Trim().Trim('"', "'")
    Write-Host "`nGenerated message:" -ForegroundColor Green
    Write-Host $message -ForegroundColor White

    # Commit changes
    git commit -m $message
    if ($LASTEXITCODE -eq 0) {
        Write-Host "Successfully committed!" -ForegroundColor Green
    }
}

Set-Alias -Name ai_commit -Value Invoke-AICommit

To activate: Run . $PROFILE or restart your PowerShell session


Usage Examples

Basic Usage

# 1. Make changes to your code
echo "console.log('Hello');" >> app.js

# 2. Stage the changes
git add app.js

# 3. Generate AI commit message and commit
ai_commit

Output:

Generating commit message...

Generated message:
feat: add hello world console log

Successfully committed!

Customization Options

Using Different AI Providers

Replace the --provider and --model-id flags with your preferred provider. You can find supported providers and models in here.

Anthropic (Claude):

--provider=claude \
--model-id="claude-haiku-4-5"

GitHub Copilot:

--provider=github_copilot \
--model-id="gpt-5-mini"

Google (Gemini):

--provider=google \
--model-id="gemini-2.5-flash"

Modifying the Agent Prompt

To adjust commit message style:

  1. Clone the agent config file from the URL used in the script to your local machine
  2. Modify the system prompt rules
  3. Update the --agent-config flag to point to your modified file

Examples: Add emoji support, change description style, require ticket numbers


Advanced: Adding Confirmation Prompt

To add a confirmation step before committing, modify the function:

Bash

# Add before git commit:
read -p "Proceed? (y/n) " -n 1 -r && echo
[[ ! $REPLY =~ ^[Yy]$ ]] && { echo "Cancelled"; return 1; }

PowerShell

# Add before git commit:
$confirm = Read-Host "Proceed? (y/n)"
if ($confirm -ne 'y') { Write-Host "Cancelled"; return }

Additional Resources


Created with AgentCrew 🤖