跳到主要内容
Git-AI:追踪 AI 生成代码的 Git 扩展工具 | 极客日志
编程语言 AI
Git-AI:追踪 AI 生成代码的 Git 扩展工具 Git-AI 是一款基于 Git Note 机制的扩展工具,旨在解决 AI 编程时代代码归属追溯的难题。它支持主流 AI 助手如 Cursor、Copilot 等,能够记录代码生成时的 Prompt 及模型信息,并在工作流变更中保持数据完整性。通过命令行或 IDE 集成,开发者可清晰区分人工与 AI 贡献,便于团队审计、合规检查及最佳实践沉淀。配置简单,兼容现有 Git 流程,是 AI 辅助开发环境下的必要基础设施。
Git-AI:追踪 AI 生成代码的 Git 扩展工具
前言
在 AI 编程助手百花齐放的今天,Claude Code、Cursor、GitHub Copilot、Gemini CLI 等工具已经成为开发者的标配。但随之而来的一个核心问题是:我们如何知道代码库中哪些代码是 AI 生成的?
这不仅仅是一个技术问题,更是关乎代码审计、团队协作、质量追踪的重要议题。
今天要介绍的 Git-AI,正是为解决这一痛点而生的工具。
什么是 Git-AI
项目概述
Git-AI 是一个 Git 扩展工具,专门用于追踪代码仓库中 AI 生成的代码。它的核心理念是:在多 Agent 时代,团队需要一种厂商无关的方式来追踪 AI 对代码库的影响。
为什么需要 Git-AI
在没有 Git-AI 之前,我们面临的困境很明显:
无法区分代码来源 :哪些是人写的?哪些是 AI 生成的?
丢失上下文 :生成代码的 prompt 是什么?用的哪个模型?
工作流断裂 :rebase、merge、squash 后,AI 归属信息丢失
多工具混乱 :团队成员用不同 AI 工具,无法统一追踪
核心功能解析
AI Blame - 代码归属追溯
类似于 git blame,Git-AI 提供了 git-ai blame 命令,可以清晰展示每一行代码的来源:
git-ai blame src/main.rs
输出示例:
Line 1 -15 [Human] Initial setup
Line 16 -45 [Claude Code] API endpoint implementation
Line 46 -80 [Cursor/GPT-4] Error handling logic
Line - Configuration tweaks
81
95
[Human]
Prompt 存储与关联 Git-AI 不仅追踪代码,还保存生成该代码的 prompt。这意味着未来可以回溯'为什么这段代码是这样写的',团队可以复用高质量的 prompt,Code Review 时也有更多上下文。
跨工作流保持归属 Git-AI 的一大亮点是归属信息能够穿越各种 Git 操作:
Git 操作 传统方式 Git-AI merge 归属丢失 ✅ 保持 rebase 归属丢失 ✅ 保持 squash 归属丢失 ✅ 保持 cherry-pick 归属丢失 ✅ 保持
支持的 AI 编程工具 Git-AI 采用厂商无关的设计,目前支持主流的 AI 编程工具:
AI 工具 代码归属 Prompt 保存 Cursor (>1.7) ✅ ✅ Claude Code ✅ ✅ GitHub Copilot (VSCode) ✅ ✅ Google Gemini CLI ✅ ✅ Continue CLI ✅ ✅ OpenCode ✅ ✅ Atlassian RovoDev ✅ ✅ Windsurf 🔄 开发中 🔄 Augment Code 🔄 开发中 🔄
技术原理剖析
工作流程
代码生成阶段 当你使用 Claude Code、Cursor 等工具生成代码时,这些工具会调用 Git-AI 的 API,标记插入的行为为 AI 生成。
提交阶段 在 git commit 时,Git-AI 会收集所有 AI 归属标记,将归属信息保存到 Git Note 中。Git Note 与 commit 绑定,但不影响 commit 本身。
查询阶段 通过 git-ai blame 等命令,读取 Git Note 中的归属信息,展示给用户。
Git Note 机制 Git-AI 选择使用 Git 原生的 Note 机制存储归属信息,这是一个精妙的设计:
不污染 commit 历史
可以独立同步/备份
向后兼容,不影响现有工作流
标准规范 Git-AI 制定了开放标准 Git AI Standard v3.0.0,定义了归属信息的格式,便于第三方工具集成、跨平台数据交换及生态系统扩展。
快速上手
安装 Mac / Linux / Windows (WSL) :
curl -sSL https://usegitai.com/install.sh | bash
powershell -NoProfile -ExecutionPolicy Bypass -Command "irm http://usegitai.com/install.ps1 | iex"
Successfully installed git-ai into /Users/xxx/.git-ai/bin ✓
Claude Code: Hooks updated ✓
Cursor: Hooks updated ✓
VS Code: Hooks already up to date ✓
Gemini: Hooks updated
重要 :安装完成后,需要重启终端或执行以下命令使 PATH 生效。
VSCode/Cursor 扩展 :如果看到提示无法自动安装扩展,需要手动安装:Cursor 在扩展商店搜索 git-ai-vscode;VSCode 访问官方插件市场。
使用前提 Git-AI 需要在有提交历史的仓库中才能正常工作:
git init
git-ai stats
git init
git add .
git commit -m "initial commit"
git-ai stats
零配置使用 安装完成后,无需任何配置,Git-AI 会自动与支持的 AI 工具集成。
git add .
git commit -m "feat: add user authentication"
git-ai blame src/auth.py
常用命令
命令格式说明 :git ai 和 git-ai 两种写法都可以执行命令,但推荐统一使用 git-ai(带连字符)。
git-ai blame <file>
git-ai stats
git-ai stats <commit-hash>
git-ai diff<commit-hash>
git-ai show-prompt <prompt-id>
git-ai show <commit-hash>
git-ai status
git-ai install-hooks
git-ai --help
企业级应用场景
代码审计与合规
代码库中有多少比例是 AI 生成的?
哪些模块 AI 参与度最高?
AI 代码的质量如何?(通过 bug 率关联分析)
团队协作洞察 通过 Stats Bot(早期访问阶段),可以获得:
PR 级别的 AI 代码占比
不同 Agent+Model 组合的代码接受率
AI 代码的'半衰期'(被修改/删除的速度)
最佳实践沉淀 高质量的 prompt 会被保留,团队可以建立 prompt 知识库,新人快速学习有效的 AI 协作方式,持续优化 AI 使用策略。
与传统 git blame 的对比 维度 git blame git-ai blame 追踪对象 人 人 + AI Agent + 模型 信息粒度 谁在什么时候改的 谁/什么 AI 用什么 prompt 生成的 上下文 commit message commit message + prompt 跨操作保持 部分丢失 完整保持 统计分析 基础 丰富的 AI 洞察
未来展望
更多 Agent 支持 Git-AI 团队正在积极与更多 AI 工具厂商合作:Windsurf、Augment Code、OpenAI Codex、JetBrains IDE 系列等。
深度分析能力 未来可能的功能包括 AI 代码质量评分、跨仓库 AI 使用模式分析、自动识别高风险 AI 代码。
开放生态 基于 Git AI Standard,第三方可以构建自己的分析工具、集成到 CI/CD 流程、开发 IDE 插件。
实战踩坑指南 在实际使用 Git-AI 的过程中,可能会遇到一些问题,这里整理出来供大家参考。
安装后命令不生效 现象 :安装完成后执行 git-ai 提示 command not found。
原因 :安装脚本修改了 ~/.zshrc,但当前终端会话没有加载新配置。
Cursor/VSCode 扩展需要手动安装 现象 :安装日志显示警告:⚠ Cursor: Unable to automatically install extension
原因 :git-ai 无法自动在 GUI 编辑器中安装扩展。
打开 Cursor/VSCode
按 Cmd+Shift+X 打开扩展面板
搜索 git-ai 或 git-ai-vscode
点击安装
重启编辑器 (很重要!)
安装扩展后 AI 代码仍然不被追踪 现象 :用 Cursor AI 改了代码,提交后 git-ai stats 显示 100% 人工。
扩展安装后没有重启 Cursor
之前的提交不会被追溯标记
完全退出 Cursor (Cmd+Q),重新打开
用 AI 做一些修改
提交前检查 git-ai 是否捕捉到:git-ai status,应该能看到类似 you ███████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ai 的输出
确认有捕捉到后再提交:git add . && git commit -m "test"
SourceTree 等 GUI 工具提交后丢失 AI 追踪 现象 :用 SourceTree 提交后,git-ai stats 显示 0% AI。
原因 :Git-AI 依赖 Git Hooks 机制,某些 GUI 工具可能跳过 hooks、使用不同的 git 环境或有禁用 hooks 的设置。
检查 SourceTree 设置,确保 hooks 未被禁用
推荐使用命令行或 Cursor/VSCode 内置 Git 面板提交
提交方式 兼容性 命令行 git commit ✅ 完全兼容 Cursor/VSCode Git 面板 ✅ 完全兼容 SourceTree ⚠️ 需确认 hooks 设置 GitHub Desktop ⚠️ 需测试
git ai --help 不工作现象 :git ai --help 输出 No manual entry for git-ai
原因 :git ai --help 会被 git 解释为查看 man page,而不是执行 git-ai --help。
Git Hook 原理详解
什么是 Git Hooks? Git Hooks(钩子)是 Git 内置的一套事件触发机制。简单来说,当你执行某个 Git 操作时,Git 会检查 .git/hooks/ 目录下有没有对应名字的脚本,如果有,就自动执行它。
常见的 Git Hooks 类型 Hook 名称 触发时机 用途示例 pre-commit执行 git commit 之前 代码格式检查、lint、阻止提交 prepare-commit-msg生成默认提交信息后 自动添加分支名或 issue 编号 commit-msg用户编辑提交信息后 校验提交信息格式 post-commit执行 git commit 之后 发送通知、记录统计信息 pre-push执行 git push 之前 运行测试、阻止推送 post-merge执行 git merge 之后 自动安装依赖 post-checkout执行 git checkout 之后 清理临时文件
Git-AI 使用了哪些 Hooks?
pre-commit :在 commit 前准备和验证 AI 归属数据
post-commit :在 commit 后将 AI 归属信息写入 Git Note
查看 Hook 脚本内容 安装 git-ai 后,你可以查看它创建的 hook 脚本:
cat .git/hooks/post-commit
#!/bin/sh
exec git-ai hook post-commit "$@ "
这个脚本非常简洁——它只是调用 git-ai hook post-commit 命令,真正的逻辑在 git-ai 程序内部处理。
为什么 SourceTree 等工具可能不触发 Hook? 场景 Hook 是否触发 原因 命令行 git commit ✅ 触发 标准 Git 行为 VSCode/Cursor Git 面板 ✅ 触发 内部调用 Git 命令 SourceTree ⚠️ 可能不触发 某些版本会跳过 hooks GitHub Desktop ⚠️ 可能不触发 同上 IDE 内置 Git ✅ 一般触发 取决于具体实现
SourceTree 使用的是内嵌的 Git,而不是系统安装的 Git
某些版本为了'加速'会跳过 hooks
SourceTree 可能使用 --no-verify 参数跳过 hooks
使用命令行提交:git add . && git commit -m "your message"
使用 VSCode/Cursor 的 Git 面板提交
配置 SourceTree 使用系统 Git:SourceTree → 偏好设置 → Git → 使用系统 Git
手动检查 Hooks 是否正常
ls -la .git/hooks/
ls -la .git/hooks/post-commit
chmod +x .git/hooks/post-commit
.git/hooks/post-commit
常见问题排查
ls .git/hooks/post-commit
git-ai hooks install
ls -la .git/hooks/post-commit
chmod +x .git/hooks/post-commit
.git/hooks/post-commit
which git-ai
问题 4:使用了 --no-verify 跳过 hooks
git commit --no-verify -m "message"
git commit -m "message"
Prompt 存储详解
两个存储位置 存储位置 路径 内容 用途 Git Note 随 commit 存储 元数据(工具、模型、行号) 代码归属追踪 全局数据库 ~/.git-ai/internal/db完整对话(prompt + 回复) Prompt 分析
Git Commit 与 Git Note 的关系 对比 Git Commit Git Note 存什么 代码变更、作者、时间、消息 额外的附加信息(AI 归属) 是否影响 commit hash ✅ 会影响 ❌ 不影响 是否必须 ✅ 必须 ❌ 可选 默认是否 push ✅ 是 ❌ 否(需单独配置) GUI 工具支持 ✅ 支持 ❌ 大多不支持
一句话总结 :Git Note 是'贴'在 Commit 上的便签纸——不改变 commit 本身,但可以附加额外信息。
查看 Git Note 中的归属信息 git-ai show <commit-hash>
git notes --ref=refs/notes/ai list
git notes --ref=refs/notes/ai show <commit-hash>
git config notes.displayRef refs/notes/ai
然后 git log 就能直接看到 AI 归属信息。
git config --unset notes.displayRef
git log --notes=refs/notes/ai
git log --no-notes
多人协同:Git Notes 的推送与同步
理解 Commit 与 Note 的推送机制
git commit 时 Note 自动附加(不需要额外操作)
git push 时 Note 不会自动推送(需要配置或手动推送)
Notes 的存储位置详解
cat .git/refs/notes/ai
git cat-file -p $(cat .git/refs/notes/ai)
手动推送和拉取 Notes
git push origin refs/notes/ai
git fetch origin refs/notes/ai:refs/notes/ai
配置自动推送/拉取(推荐) 配置一次,以后每次 push/fetch 自动包含 Notes:
git config --add remote.origin.push refs/notes/ai
git config --add remote.origin.fetch +refs/notes/ai:refs/notes/ai
[remote "origin"]
url = [email protected] :xxx/xxx.git
fetch = +refs/heads/*:refs/remotes/origin/*
fetch = +refs/notes/ai:refs/notes/ai
push = refs/notes/ai
多人协同完整流程 场景 :团队 3 人协作,都使用 AI 辅助编程,想要共享 AI 代码统计。
Step 1:每个成员配置自动同步(每人执行一次)
git config --add remote.origin.push refs/notes/ai
git config --add remote.origin.fetch +refs/notes/ai:refs/notes/ai
git add .
git commit -m "feat: 新功能"
git push
git pull
git-ai stats
git clone <repo-url>
cd <project>
git config --add remote.origin.push refs/notes/ai
git config --add remote.origin.fetch +refs/notes/ai:refs/notes/ai
git fetch origin refs/notes/ai:refs/notes/ai
git-ai stats
不配置同步会怎样? 场景 git-ai stats 显示未配置同步 只有你自己的 AI 代码统计 配置了同步 整个团队的 AI 代码统计
各工具对 Git Notes 的支持 工具 能否查看 Git Notes 命令行 git log (配置后) ✅ 能 git-ai show✅ 能 SourceTree ❌ 不支持 GitHub 网页 ❌ 默认不显示 GitLens (VSCode) ⚠️ 有限支持
Git Notes 是'隐藏'的元数据,专门给工具(如 git-ai)使用,大多数 GUI 工具看不到。这也是为什么 git-ai 提供了专门的命令来读取和展示这些信息。
查看使用了哪些 AI 工具 git-ai stats 只显示整体的 AI 代码比例,如果想知道具体使用了哪些 AI 工具(Claude、Cursor、Copilot 等),有以下方法:
方法 1:查看某个文件的 AI 归属详情 输出会显示每一行是哪个 AI 生成的,包括工具和模型信息。
方法 2:查看某个 commit 的 AI 信息
git-ai show <commit-hash>
git-ai show HEAD
方法 3:通过 prompts 数据库查询
git-ai prompts
git-ai prompts list
git-ai prompts exec "SELECT DISTINCT json_extract(agent_id, '$.tool') as tool, json_extract(agent_id, '$.model') as model FROM prompts"
方法 4:直接查看 Git Note 原始内容
git notes --ref=refs/notes/ai show HEAD
{
"schema_version" : "authorship/3.0.0" ,
"prompts" : {
"0fd5eeb3d01edae0" : {
"agent_id" : {
"tool" : "claude" , ← AI 工具名称
"model" : "claude-opus-4-5-20251101" ← 具体模型版本
}
} ,
"cce7dc1177030070" : {
"agent_id" : {
"tool" : "cursor" ,
"model" : "gpt-4"
}
}
}
}
常见 AI 工具标识 tool 字段 对应工具 claudeClaude Code CLI cursorCursor 编辑器 copilotGitHub Copilot continueContinue 插件 geminiGemini CLI
查看完整的 Prompt 对话 Git Note 里只有精简的元数据,完整的 prompt 对话存储在全局数据库:
git-ai prompts
git-ai prompts list
git-ai prompts exec "SELECT messages FROM prompts WHERE"
用户输入的 prompt
AI 的回复
工具调用记录(Read、Edit 等)
全局数据库文件说明 ~/.git-ai/
├── bin/
├── config.json
└── internal/
├── db
├── db-shm
└── db-wal
brew install --cask db-browser-for-sqlite
cp ~/.git-ai/internal/db ~/Desktop/git-ai-prompts.db
项目目录下的 prompts.db 运行 git-ai prompts 会在项目目录生成 prompts.db,这是临时分析文件,建议加入 .gitignore:
echo "prompts.db" >> .gitignore
总结 Git-AI 填补了 AI 编程时代的一个重要空白:追踪和管理 AI 生成的代码。
透明性 :清晰知道代码的来源
可追溯 :保留生成代码的上下文(prompt、模型等)
工程化 :与 Git 深度集成,支持真实工作流
标准化 :厂商无关的开放标准
对于正在大规模采用 AI 编程工具的团队来说,Git-AI 不是一个"nice to have",而是即将成为"must have"的基础设施。
相关免费在线工具 RSA密钥对生成器 生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online
Mermaid 预览与可视化编辑 基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online
Base64 字符串编码/解码 将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
Base64 文件转换器 将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online
Markdown转HTML 将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML转Markdown 互为补充。 在线工具,Markdown转HTML在线工具,online
HTML转Markdown 将 HTML 片段转为 GitHub Flavored Markdown,支持标题、列表、链接、代码块与表格等;浏览器内处理,可链接预填。 在线工具,HTML转Markdown在线工具,online