Git AI教程:追踪AI生成代码的Git扩展工具怎么用

📅 2026-08-03 · 🏷️ Git · 代码治理 · AI编程

项目概览

GitHub: acunniffe/git-ai · 语言: Rust · 开源协议: MIT

Git AI是什么?

Git AI是一个开源的Git扩展工具,用于自动追踪和标记AI生成的代码。它在你正常的Git工作流中静默工作,记录哪些代码是AI写的、哪些是人类写的,让你对代码库的AI贡献比例一目了然。

随着AI编程工具(Claude Code、Cursor、Copilot等)的普及,代码库中AI生成代码的比例越来越高。Git AI帮你回答一个关键问题:我的代码库里到底有多少是AI写的?

核心功能

安装

# 一键安装(Rust编译)
cargo install git-ai

# 或下载预编译二进制
# macOS
brew install git-ai
# Windows
scoop install git-ai

# 验证安装
git ai --version

配置

在项目中启用

cd your-project

# 初始化Git AI追踪
git ai init

# 配置要追踪的AI工具
git ai config set tools claude-code,cursor,copilot,aider

# 安装Git hooks(自动标记AI commit)
git ai hooks install

自动标记原理

Git AI通过以下方式识别AI生成的代码:

  1. Commit消息模式:识别Claude Code、Cursor等工具的commit消息格式
  2. Author信息:AI工具的commit通常有特定author标签
  3. 文件变更模式:AI生成代码的变更模式与人类不同
  4. 手动标记git ai mark --ai手动标记commit

使用方法

查看AI代码占比

git ai stats

# 输出示例:
# ┌─────────────────────────────────────┐
# │  AI Code Contribution: 34.2%        │
# │  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ │
# │  Total lines:     45,230            │
# │  AI generated:    15,472            │
# │  Human written:   29,758            │
# │  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ │
# │  By tool:                            │
# │    Claude Code:   8,231 (18.2%)     │
# │    Cursor:        4,102 (9.1%)      │
# │    Copilot:       3,139 (6.9%)      │
# └─────────────────────────────────────┘

按目录分析

git ai stats --by-directory

# src/
#   auth/      AI: 45% (1,200 lines)
#   api/       AI: 12% (300 lines)
#   utils/     AI: 78% (2,100 lines)
# tests/
#   AI: 89% (3,400 lines) ← AI写测试很多

生成报告

# 生成HTML报告
git ai report --format html --output ai-code-report.html

# 生成JSON(CI/CD集成用)
git ai report --format json --output report.json

如果你在用Claude Code,可以配合Claude Code完整部署指南使用。对于想了解AI编程工具对比的,GitHub Copilot vs Claude Code vs Codex对比是很好的参考。

CI/CD集成

在GitHub Actions中自动追踪AI代码:

# .github/workflows/ai-track.yml
name: AI Code Tracking
on: [pull_request]
jobs:
  track:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Install git-ai
        run: cargo install git-ai
      - name: Generate report
        run: git ai report --format json --output pr-report.json
      - name: Comment on PR
        uses: actions/github-script@v7
        with:
          script: |
            const report = require('./pr-report.json');
            github.rest.issues.createComment({
              ...context.repo,
              issue_number: context.issue.number,
              body: `🤖 AI代码占比: ${report.ai_percentage}%`
            });

使用场景

总结

Git AI是AI编程时代的必备治理工具。随着AI生成代码占比不断上升,知道哪些代码是AI写的、质量如何,对代码审计和质量管控越来越重要。Git AI以零侵入的方式帮你实现这一点。

📚 相关阅读

Claude Code完整部署指南:从零搭建AI编程助手

GitHub Copilot vs Claude Code vs Codex:2026年AI编程助手三大天王对比

📚 常见问题

Git AI是什么?

GitHub: acunniffe/git-ai · 语言: Rust · 开源协议: MIT

如何上手Git AI?

Commit消息模式:识别Claude Code、Cursor等工具的commit消息格式