Lesson 11 of 30 ~15 min
Course progress
0%

Git Workflow Integration

Používejte Claude pro commit messages, PR reviews, a git operace.

Git je srdce moderního vývoje. Claude může výrazně zlepšit váš git workflow.

Automatické commit messages

Script pro generování commit messages

#!/bin/bash
# scripts/git-commit-ai.sh

# Get staged diff
DIFF=$(git diff --cached)

if [ -z "$DIFF" ]; then
    echo "No staged changes"
    exit 1
fi

# Ask Claude for commit message
MESSAGE=$(python3 << EOF
from anthropic import Anthropic
import sys

client = Anthropic()

diff = """$DIFF"""

response = client.messages.create(
    model="claude-sonnet-4-5-20250101",  # Sonnet stačí
    max_tokens=200,
    messages=[{
        "role": "user",
        "content": f"""Generate a conventional commit message for this diff.
Format: <type>(<scope>): <description>

Types: feat, fix, docs, style, refactor, test, chore

Diff:
\{diff\}

Return ONLY the commit message, nothing else."""
    }]
)

print(response.content[0].text.strip())
EOF
)

echo "Suggested commit message:"
echo "$MESSAGE"
echo ""
read -p "Use this message? (y/n/e for edit): " choice

case $choice in
    y) git commit -m "$MESSAGE" ;;
    e) git commit -e -m "$MESSAGE" ;;
    *) echo "Aborted" ;;
esac

Git alias

# ~/.gitconfig
[alias]
    ai-commit = "!bash ~/scripts/git-commit-ai.sh"
    
# Použití
git add .
git ai-commit

PR Reviews s Claude

Review script

#!/usr/bin/env python3
# scripts/pr-review.py

import subprocess
from anthropic import Anthropic

def get_pr_diff(base="main"):
    result = subprocess.run(
        ["git", "diff", base],
        capture_output=True,
        text=True
    )
    return result.stdout

def review_pr(diff):
    client = Anthropic()
    
    response = client.messages.create(
        model="claude-opus-4-5-20250101",  # Opus pro kvalitní review
        max_tokens=4000,
        thinking={"type": "enabled", "budget_tokens": 10000},
        messages=[{
            "role": "user",
            "content": f"""Review this PR diff. Focus on:

1. **Bugs**: Logic errors, edge cases, null handling
2. **Security**: Vulnerabilities, injection, auth issues
3. **Performance**: N+1 queries, unnecessary loops
4. **Maintainability**: Code clarity, naming, structure

Format your response as:
## Critical Issues
## Suggestions  
## Positive Notes

Diff:

{diff}

        }]
    )
    
    return response.content

if __name__ == "__main__":
    import sys
    base = sys.argv[1] if len(sys.argv) > 1 else "main"
    diff = get_pr_diff(base)
    
    if not diff:
        print("No changes from", base)
    else:
        review = review_pr(diff)
        for block in review:
            if hasattr(block, 'text'):
                print(block.text)

Git Hooks

Pre-commit hook

#!/bin/bash
# .git/hooks/pre-commit

# Check for secrets
if git diff --cached | grep -E "(api_key|password|secret)" > /dev/null; then
    echo "⚠️  Possible secret detected in commit!"
    echo "Running Claude security check..."
    
    python3 scripts/security-check.py
    
    if [ $? -ne 0 ]; then
        echo "❌ Security check failed. Commit aborted."
        exit 1
    fi
fi

Prepare-commit-msg hook

#!/bin/bash
# .git/hooks/prepare-commit-msg

COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2

# Only for regular commits (not merges, etc.)
if [ -z "$COMMIT_SOURCE" ]; then
    # Generate AI suggestion
    SUGGESTION=$(git diff --cached | head -c 10000 | python3 scripts/suggest-message.py)
    
    # Prepend to commit message
    echo "$SUGGESTION" > "$COMMIT_MSG_FILE.tmp"
    echo "" >> "$COMMIT_MSG_FILE.tmp"
    echo "# AI suggested message above. Edit as needed." >> "$COMMIT_MSG_FILE.tmp"
    cat "$COMMIT_MSG_FILE" >> "$COMMIT_MSG_FILE.tmp"
    mv "$COMMIT_MSG_FILE.tmp" "$COMMIT_MSG_FILE"
fi

Branch naming

# scripts/suggest-branch.py
from anthropic import Anthropic

client = Anthropic()

task = input("Describe your task: ")

response = client.messages.create(
    model="claude-haiku-3-5",  # Haiku stačí
    max_tokens=50,
    messages=[{
        "role": "user",
        "content": f"""Suggest a git branch name for: {task}

Format: <type>/<short-description>
Types: feature, fix, refactor, docs, test

Return ONLY the branch name."""
    }]
)

branch = response.content[0].text.strip()
print(f"Suggested: {branch}")
print(f"Run: git checkout -b {branch}")

Changelog generation

# scripts/generate-changelog.py
import subprocess
from anthropic import Anthropic

def get_commits_since(tag):
    result = subprocess.run(
        ["git", "log", f"{tag}..HEAD", "--oneline"],
        capture_output=True,
        text=True
    )
    return result.stdout

client = Anthropic()
commits = get_commits_since("v1.0.0")

response = client.messages.create(
    model="claude-sonnet-4-5-20250101",
    max_tokens=2000,
    messages=[{
        "role": "user",
        "content": f"""Generate a changelog from these commits:

{commits}

Format:
## [Version] - Date

### Added
### Changed  
### Fixed
### Removed"""
    }]
)

print(response.content[0].text)

Tyto nástroje vám ušetří hodiny při každodenní práci s gitem.