Prevent AI Attribution in Git Commits with Pre-Commit Hooks

David Childs

Protect your git history from AI attribution with custom hooks. Learn to block AI references automatically and maintain professional commits.

As AI coding assistants become more prevalent in development workflows, they often try to add attribution to git commits. While transparency has its place, many teams prefer to maintain commit histories that focus on the changes themselves rather than the tools used to create them. Today, I'll show you how to implement git hooks that automatically prevent any AI attribution from entering your repository.

The Problem with AI Attribution in Commits

Many AI coding assistants automatically append signatures to commit messages:

  • "🤖 Generated with [Tool Name]"
  • "Co-Authored-By: AI Assistant noreply@example.com"
  • "This commit was AI-generated"

While these attributions might seem harmless, they can:

  • Clutter commit history with tool-specific metadata
  • Distract from the actual changes being made
  • Create inconsistency when some commits have attribution and others don't
  • Raise questions in professional or client-facing repositories
  • Expose tooling choices that may be considered proprietary information

Solution: Git Commit-Message Hooks

Git hooks are scripts that run automatically at specific points in the git workflow. The commit-msg hook is perfect for validating and potentially rejecting commit messages before they're finalized.

Creating the Commit-Message Hook

Here's a comprehensive hook that blocks common AI attribution patterns:

#!/bin/bash

# Pre-commit hook to prevent AI references in commit messages
# Save this as .git/hooks/commit-msg

commit_message_file=$1
commit_message=$(cat "$commit_message_file")

# List of patterns to block (case-insensitive)
blocked_patterns=(
    "claude"
    "anthropic"
    "chatgpt"
    "openai"
    "copilot"
    "Generated with"
    "AI-generated"
    "AI assisted"
    "AI-powered"
    "🤖"
    "Co-Authored-By: AI"
    "noreply@anthropic"
    "noreply@openai"
)

# Check for blocked patterns
for pattern in "${blocked_patterns[@]}"; do
    if echo "$commit_message" | grep -qi "$pattern"; then
        echo "❌ Commit blocked: Found reference to '$pattern' in commit message"
        echo ""
        echo "Your commit message contains AI/tool references that should not be included."
        echo "Please rewrite your commit message without any AI attribution."
        echo ""
        echo "Blocked commit message:"
        echo "------------------------"
        echo "$commit_message"
        echo "------------------------"
        exit 1
    fi
done

# If we get here, the commit message is clean
exit 0

Installation Methods

Method 1: Direct Installation

For immediate use in your current repository:

# Create the hook file
cat > .git/hooks/commit-msg << 'EOF'
[paste the script above]
EOF

# Make it executable
chmod +x .git/hooks/commit-msg

Method 2: Shareable Team Setup

For teams, create a .githooks directory in your repository:

# Create the directory structure
mkdir -p .githooks

# Copy the hook
cp .git/hooks/commit-msg .githooks/commit-msg

# Create an install script
cat > .githooks/install.sh << 'EOF'
#!/bin/bash

echo "Installing git hooks..."

GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)

if [ -z "$GIT_DIR" ]; then
    echo "❌ Error: Not in a git repository"
    exit 1
fi

cp .githooks/commit-msg "$GIT_DIR/hooks/commit-msg"
chmod +x "$GIT_DIR/hooks/commit-msg"

echo "✅ Git hooks installed successfully!"
EOF

chmod +x .githooks/install.sh

Team members can then run:

bash .githooks/install.sh

Testing Your Hook

Once installed, test that it's working correctly:

# This should fail
git commit -m "Fix bug (Generated with AI assistance)"

# Output:
# ❌ Commit blocked: Found reference to 'Generated with' in commit message
# Your commit message contains AI/tool references that should not be included.

# This should succeed
git commit -m "Fix authentication bug in login flow"

Customizing the Patterns

You can easily customize the blocked patterns for your team's needs. Common additions might include:

blocked_patterns=(
    # Specific tool names
    "cursor"
    "tabnine"
    "kite"
    "sourcegraph"
    
    # Generic AI terms
    "artificial intelligence"
    "machine learning"
    "LLM"
    "language model"
    
    # Attribution patterns
    "assisted by"
    "powered by"
    "created with"
    "built using"
)

Advanced Features

Adding Warning Mode

Instead of blocking commits entirely, you might want to warn but allow override:

if echo "$commit_message" | grep -qi "$pattern"; then
    echo "⚠️  Warning: Found reference to '$pattern' in commit message"
    echo "Continue anyway? (y/n)"
    read -r response
    if [[ ! "$response" =~ ^[Yy]$ ]]; then
        exit 1
    fi
fi

Logging Blocked Attempts

Track when commits are blocked:

if echo "$commit_message" | grep -qi "$pattern"; then
    echo "$(date): Blocked pattern '$pattern'" >> .git/blocked-commits.log
    # ... rest of blocking logic
fi

Exceptions for Specific Files

Allow AI attribution in certain contexts (like AI research projects):

# Check if we're in an AI-specific project
if [ -f ".ai-project" ]; then
    exit 0  # Skip all checks
fi

Team Deployment Strategies

1. Repository Template

Create a template repository with hooks pre-configured that teams can use for new projects.

2. Git Config Core.HooksPath

Configure git to use a central hooks directory:

git config core.hooksPath .githooks

3. Pre-Commit Framework

Use the pre-commit framework for more sophisticated hook management:

# .pre-commit-config.yaml
repos:
  - repo: local
    hooks:
      - id: no-ai-attribution
        name: Block AI Attribution
        entry: .githooks/commit-msg
        language: script
        stages: [commit-msg]

Handling Edge Cases

Emergency Override

Sometimes you might need to bypass the hook:

# Use with caution - only in emergencies
git commit --no-verify -m "Your message"

Fixing Historical Commits

If you need to clean up existing commits:

# Rewrite history to remove AI references
git filter-branch --msg-filter 'sed -e "/🤖/d" -e "/Generated with/d"' HEAD~10..HEAD

Best Practices

  1. Document your hooks - Include a README in .githooks/ explaining what each hook does
  2. Version control your hooks - Keep them in the repository for consistency
  3. Test thoroughly - Ensure hooks don't block legitimate commits
  4. Keep patterns updated - Add new AI tools as they emerge
  5. Provide clear error messages - Help developers understand why commits are blocked

Conclusion

Git hooks provide a powerful way to maintain consistent, professional commit histories free from AI attribution. By implementing these hooks, teams can use AI tools effectively while maintaining complete control over their repository metadata.

The approach scales from individual developers to large teams, and can be customized to match any organization's specific needs. Whether you're working on open source projects, client deliverables, or internal tools, clean commit messages help maintain focus on what really matters: the code changes themselves.

Remember, the goal isn't to hide AI usage, but to maintain clean, focused commit histories that emphasize the changes made rather than the tools used to make them. This creates more readable logs, cleaner git blame output, and more professional repository presentations.

Share this article

DC

David Childs

Consulting Systems Engineer with over 10 years of experience building scalable infrastructure and helping organizations optimize their technology stack.

Related Articles