
Repomix 基础使用指南将整个代码仓库打包为 AI 友好的单一文件【免费下载链接】repomix Repomix is a powerful tool that packs your entire repository into a single, AI-friendly file. Perfect for when you need to feed your codebase to Large Language Models (LLMs) or other AI tools like Claude, ChatGPT, DeepSeek, Perplexity, Gemini, Gemma, Llama, Grok, and more.项目地址: https://gitcode.com/GitHub_Trending/rep/repomixRepomix 是一款把整个代码仓库打包成单一、AI 友好文件的命令行工具适合将代码库喂给 Claude、ChatGPT、DeepSeek、Perplexity、Gemini、Gemma、Llama、Grok 等大语言模型LLM或 AI 工具。本文以官方基础使用指南为核心系统讲解 Repomix 的快速上手、常见场景目录打包、文件筛选、远程仓库、stdin 文件列表、代码压缩、Git 集成、Token 统计、输出拆分、输出格式与全部关键命令行选项并结合当前仓库源码说明各功能背后的实现原理读完即可在真实项目中完成一次高效的 AI 代码分析。Quick Start一条命令打包整个仓库在项目根目录直接运行repomix即可在当前目录生成一个repomix-output.xml文件包含整个仓库的 AI 友好格式内容。之后你可以把这个文件发送给 AI 助手并附上类似提示词This file contains all the files in the repository combined into one. I want to refactor the code, so please review it first.Repomix 的默认输出文件路径与格式在 configSchema.ts 中定义xml风格默认输出repomix-output.xmlmarkdown对应repomix-output.mdplain对应repomix-output.txtjson对应repomix-output.json见 defaultFilePathMap。常见使用场景打包指定目录repomix path/to/directory只需把路径作为位置参数传入即可只打包该目录。只包含指定文件Include使用 glob 模式精确筛选要打包的文件repomix --include src/**/*.ts,**/*.md--include接收逗号分隔的多个 glob 模式。在源码中这些模式会进入文件收集流程fileCollect.ts与--ignore共同决定最终打包范围。排除指定文件Ignorerepomix --ignore **/*.log,tmp/--ignore同样接收逗号分隔模式可排除日志、构建产物等无需打包的内容。注意 Repomix 默认还遵循.gitignore、.ignore和.repomixignore规则对应--no-gitignore、--no-dot-ignore参数可关闭。将输出拆分为多个文件Split Output面对大型代码库打包结果可能超过部分 AI 工具的附件大小限制例如 Google AI Studio 的 1MB 限制。使用--split-output自动拆分repomix --split-output 1mb这会生成编号文件repomix-output.1.xmlrepomix-output.2.xmlrepomix-output.3.xml大小可带单位指定500kb、1mb、2mb、1.5mb等支持小数。[!NOTE] 文件按顶层目录分组以保持上下文完整。单个文件或目录绝不会被拆分到多个输出文件中。这个绝不拆分单文件/单目录的保证有明确的源码实现支撑在 outputSplit.ts 中generateSplitOutputParts先把所有文件按顶层目录入口分组成OutputSplitGroup当一个分组连同已累积内容超过上限时先结束当前分片再重试如果该分组单独仍超限则通过subdivideSplitGroup按目录层级逐级细分src→src/a、src/b…直到细分为单个文件为止此时若仍超限才抛出 A single file cannot be split across parts 错误见 outputSplit.ts。另外非首个分片会自动关闭 git diffs/logs避免大段重复内容见 makeChunkConfig。远程仓库打包无需手动 cloneRepomix 可直接处理远程 Git 仓库# 使用 GitHub URL repomix --remote https://github.com/user/repo # 使用简写 repomix --remote user/repo # 不带 --remote 的简写自动检测 repomix user/repo # 指定分支 / tag / commit repomix --remote user/repo --remote-branch main repomix --remote user/repo --remote-branch 935b695--remote-branch支持分支名、tag 或 commit hash也可以直接传分支 URLhttps://github.com/user/repo/tree/main或 commit URLhttps://github.com/user/repo/commit/hash。从源码看远程处理前会先通过 HEAD-only 的git ls-remote探测仓库是否可达gitRemoteHandle.ts这种探测方式即使对大型仓库也很轻量。[!NOTE] 出于安全考虑远程仓库中的配置文件repomix.config.*默认不会被加载防止不信任仓库通过配置文件执行代码。你的全局配置和 CLI 参数仍然生效。如需信任远程仓库配置使用--remote-trust-config交互式终端会先展示配置并请求确认。 使用--config搭配--remote时必须传绝对路径如--config /home/user/repomix.config.json。文件列表输入stdin--stdin允许你通过管道把文件路径列表喂给 Repomix获得最大的文件选择灵活性# 使用 find 命令 find src -name *.ts -type f | repomix --stdin # 使用 git 获取已跟踪文件 git ls-files *.ts | repomix --stdin # 使用 ripgrep (rg) 查找文件 rg --files --type ts | repomix --stdin # 使用 grep 查找包含特定内容的文件 grep -l TODO **/*.ts | repomix --stdin # 使用 ripgrep 查找包含特定内容的文件 rg -l TODO|FIXME --type ts | repomix --stdin # 使用 sharkdp/fd 查找文件 fd -e ts | repomix --stdin # 使用 fzf 从所有文件中挑选 fzf -m | repomix --stdin # 交互式文件选择fzf find . -name *.ts -type f | fzf -m | repomix --stdin # 使用 ls glob ls src/**/*.ts | repomix --stdin # 从包含路径列表的文件读取 cat file-list.txt | repomix --stdin # echo 直接输入 echo -e src/index.ts\nsrc/utils.ts | repomix --stdin使用--stdin时指定的文件会被有效加入 include 模式。也就是说正常的 include/ignore 行为仍然生效——通过 stdin 指定的文件如果匹配 ignore 模式依然会被排除。[!NOTE] 使用--stdin时文件路径可以是相对路径或绝对路径Repomix 会自动处理路径解析与去重。实现上fileStdin.ts 逐行读取管道输入过滤空行和以#开头的注释行filterValidLines再经resolveAndDeduplicatePaths把相对路径基于当前工作目录解析为绝对路径并用Set去重如果 stdin 是 TTY交互终端或没有有效路径会抛出明确错误提示。代码压缩Code Compression--compress通过 Tree-sitter 提取关键代码结构类、函数、接口签名在保留结构信息的同时显著降低 token 数量repomix --compress # 也可以用于远程仓库 repomix --remote yamadashy/repomix --compress例如下面这段 TypeScriptimport { ShoppingItem } from ./shopping-item; /** * Calculate the total price of shopping items */ const calculateTotal ( items: ShoppingItem[] ) { let total 0; for (const item of items) { total item.price * item.quantity; } return total; } // Shopping item interface interface Item { name: string; price: number; quantity: number; }会被压缩为实现细节用⋮----占位import { ShoppingItem } from ./shopping-item; ⋮---- /** * Calculate the total price of shopping items */ const calculateTotal ( items: ShoppingItem[] ) { ⋮---- // Shopping item interface interface Item { name: string; price: number; quantity: number; }[!NOTE] 这是实验性功能会基于用户反馈和真实使用持续改进。压缩管道的具体实现位于 parseFile.ts 与各语言的解析策略如 TypeScriptParseStrategy.ts、PythonParseStrategy.ts 等配合 queries 目录下各语言的 tree-sitter 查询提取函数、类等关键语法节点。压缩、注释移除等能力在文件处理阶段由 fileProcessContent.ts 统一调度。按文件粒度控制包含级别output.patterns--compress对所有文件统一生效而配置文件里的output.patterns允许你按 glob 逐文件控制详细程度每个条目匹配文件匹配方式与include/ignore一致并覆盖全局output.compress设置{ output: { compress: false, // 全局默认值作为兜底 patterns: [ { pattern: docs/**/*, compress: true }, { pattern: website/**/*, directoryStructureOnly: true } ] } }共三个级别完整内容默认——包含文件的完整内容压缩compress: true——内容走与--compress相同的 Tree-sitter 管道仅目录结构directoryStructureOnly: true——文件出现在目录结构中但内容块完全从输出中省略。语义规则模式按数组顺序求值首个匹配生效匹配到的模式的标记覆盖全局output.compress匹配但未设置任何标记的模式强制该文件为完整内容可用于把某些文件从全局compress中白名单化同时设置时directoryStructureOnly优先于compress无模式匹配时按全局行为处理。这是仅配置文件的选项没有对应的 CLI 标志。相关 schema 定义在 outputPatternSchema。Git 集成把 Git 信息纳入输出为 AI 分析提供开发上下文# 包含 git diffs未提交的改动 repomix --include-diffs # 包含 git commit 日志默认最近 50 条 repomix --include-logs # 指定提交数量 repomix --include-logs --include-logs-count 10 # 同时包含 diffs 和 logs repomix --include-diffs --include-logs这提供了关于以下方面的宝贵上下文近期改动git diffs 展示未提交的修改开发模式git logs 揭示哪些文件通常一起变更提交历史最近的提交信息提供开发重点的洞察文件关系理解同一提交中修改了哪些文件。实现上git log 使用 null 字符作为记录分隔符%x00即使提交信息含换行也能稳健解析见 gitLogHandle.ts默认取 50 条--include-logs-count可调默认值见 configSchema.tsgit diff 处理在 gitDiffHandle.ts 中实现。输出时Markdown 风格会为 diffs 渲染# Git Diffs、# Git Logs独立区块见 markdownStyle.ts。Token 统计优化理解代码库的 token 分布对优化 AI 交互至关重要。用--token-count-tree可视化整个项目的 token 使用情况repomix --token-count-tree这将显示带 token 计数的层级视图 Token Count Tree: ──────────────────── └── src/ (70,925 tokens) ├── cli/ (12,714 tokens) │ ├── actions/ (7,546 tokens) │ └── reporters/ (990 tokens) └── core/ (41,600 tokens) ├── file/ (10,098 tokens) └── output/ (5,808 tokens)也可以设置最小 token 阈值聚焦较大的文件repomix --token-count-tree 1000 # 只显示 1000 tokens 的文件/目录这有助于识别 token 密集文件避免超出 AI 上下文限制优化文件选择使用--include和--ignore模式规划压缩策略瞄准最大的贡献者平衡内容与上下文为 AI 分析准备代码。Token 计数基于gpt-tokenizer默认编码为o200k_baseGPT-4o 系也可通过--token-count-encoding切换为cl100k_base等见 TokenCounter.ts 与 configSchema.ts。树状结构由 buildTokenCountStructure.ts 构建每个目录节点独立存放files、tokenSum和children子目录的 token 数逐层向上汇总。输出格式XML默认repomix --style xmlXML 以层级方式组织内容file_summary元数据与 AI 使用说明、directory_structure目录树、files每个文件带path属性、可选的git_diffs/git_logs/instruction区块。XML 标签对 AI 上下文解析尤其友好——当提示词包含上下文、指令、示例等多组件时XML 标签能帮助 Claude 更准确地解析获得更高质量的产出。Markdownrepomix --style markdown输出包含# File Summary、# Directory Structure、# Files每个文件以## File: path/to/file为标题并放入代码块、以及可选的# Git Diffs、# Git Logs、# Instruction区块。模板定义在 markdownStyle.ts代码围栏分隔符会根据内容中最长的反引号串动态加长防止 Markdown 文件内容提前闭合代码块破坏输出见 calculateMarkdownDelimiter。JSONrepomix --style json以驼峰命名的层级 JSON 对象输出结构类似{ fileSummary: { generationHeader: This file is a merged representation of the entire codebase, combined into a single document by Repomix., purpose: This file contains a packed representation of the entire repositorys contents..., fileFormat: The content is organized as follows..., usageGuidelines: - This file should be treated as read-only..., notes: - Some files may have been excluded based on .gitignore, .ignore, and .repomixignore rules... }, userProvidedHeader: Custom header text if specified, directoryStructure: src/\n cli/\n cliOutput.ts\n index.ts\n config/\n configLoader.ts, files: { src/index.js: // File contents here, src/utils.js: // File contents here }, instruction: Custom instructions from instructionFilePath }适合程序化处理用 JSON 库轻松解析操作API 集成供 Web 服务和应用程序直接消费AI 工具兼容为机器学习与 AI 系统提供结构化格式数据分析用jq等工具直接提取特定信息。配合jq的典型用法# 列出所有文件路径 cat repomix-output.json | jq -r .files | keys[] # 统计文件总数 cat repomix-output.json | jq .files | keys | length # 提取特定文件内容 cat repomix-output.json | jq -r .files[README.md] cat repomix-output.json | jq -r .files[src/index.js] # 按扩展名查找文件 cat repomix-output.json | jq -r .files | keys[] | select(endswith(.ts)) # 查找包含特定文本的文件 cat repomix-output.json | jq -r .files | to_entries[] | select(.value | contains(function)) | .key # 提取目录结构 cat repomix-output.json | jq -r .directoryStructure # 获取文件摘要信息 cat repomix-output.json | jq .fileSummary.purpose cat repomix-output.json | jq -r .fileSummary.generationHeader # 提取用户提供的头部如果存在 cat repomix-output.json | jq -r .userProvidedHeader // No header provided # 生成带大小的文件列表 cat repomix-output.json | jq -r .files | to_entries[] | \(.key): \(.value | length) charactersJSON 输出的组装逻辑见 generateParsableJsonOutput。纯文本Plain Textrepomix --style plain以分隔线划分File Summary、Directory Structure、Files、Instruction区块人类可读且易被 AI 系统解析适合对格式要求极简的场景。更多选项移除注释repomix --remove-comments打包前剥离所有代码注释支持的语言范围详见 comment-removal。显示行号repomix --output-show-line-numbers输出中为每行代码加行号前缀便于 AI 引用具体代码位置。复制到剪贴板repomix --copy处理完成后自动把生成结果复制到系统剪贴板。关闭安全检查repomix --no-security-check默认情况下 Repomix 会扫描 API 密钥、密码等敏感数据基于 Secretlint 检测已知凭据格式关闭后这些内容可能进入输出请谨慎使用。安全检测实现见 securityCheck.ts。配置文件初始化配置文件repomix --init这会在当前目录生成带默认值的repomix.config.json。完整配置选项可参考 Configuration Guide。配置的结构定义在 configSchema.ts核心默认值包括默认 50MB 单文件上限、XML 默认风格、target-relative路径风格、默认启用.gitignore/.ignore/内置默认忽略模式、默认开启安全检测、默认o200k_basetoken 编码。其他关键 CLI 选项速查输入/输出选项说明--verbose启用详细调试日志显示文件处理、token 数、配置细节--quiet除错误外抑制所有控制台输出适合脚本--stdout直接输出到 stdout 而非文件抑制所有日志--stdin从 stdin 逐行读取文件路径--copy处理后复制结果到系统剪贴板-o, --output file输出文件路径默认repomix-output.xml-表示 stdout--style stylexml/markdown/json/plain默认xml--output-file-path-style style输出中的路径风格target-relative或cwd-relative默认target-relative--parsable-style转义特殊字符保证 XML/Markdown 合法性--no-file-summary省略文件摘要区块--no-directory-structure省略目录树可视化--no-files只生成元数据不含文件内容适合仓库分析--remove-empty-lines移除所有文件的空行--truncate-base64截断长 base64 数据串以减小输出体积--header-text text在输出开头加入自定义文本--instruction-file-path path指定包含自定义指令的文件路径--include-empty-directories在目录结构中包含空文件夹--include-full-directory-structure显示完整目录树包括未匹配--include的文件--no-git-sort-by-changes关闭按 git 变更频率排序默认变更最多的文件在前文件选择选项说明--include patterns只包含匹配的 glob 模式逗号分隔-i, --ignore patterns额外排除的模式逗号分隔--no-gitignore不使用.gitignore规则--no-dot-ignore不使用.ignore规则--no-default-patterns不应用内置忽略模式node_modules、.git、构建目录等远程仓库选项说明--remote url克隆并打包远程仓库GitHub URL 或user/repo--remote-branch name指定分支、tag 或 commit默认仓库默认分支--remote-trust-config信任并加载远程仓库配置默认关闭交互终端会先展示配置配置选项说明-c, --config path使用自定义配置文件--init创建默认repomix.config.json--global配合--init在主目录而非当前目录创建配置安全--no-security-check—— 跳过 API 密钥、密码等敏感数据扫描。Token 统计--token-count-encoding encoding—— 计数编码默认o200k_base--token-budget number—— 打包输出超过 N tokens 时以非零退出码失败适合在 CI 管道和 Agent 工作流中作为护栏输出仍会生成仅退出码指示超限。MCP--mcp—— 以 Model Context Protocol 服务器模式运行供 AI 工具直接调用--sandbox [dir]——配合--mcp把 MCP 文件工具限定在工作区目录内拒绝绝对/宿主路径并禁用远程打包、技能生成与附加外部输出。Agent Skills 生成--skill-generate [name]—— 生成 Claude Agent Skills 格式输出到.claude/skills/name/--skill-project-name name、--skill-output path与-f, --force跳过所有确认提示配套使用。Watch 模式-w, --watch—— 监听文件变化并自动重新打包防抖 300ms每次重建打印时间戳CtrlC停止。注意 Watch 模式仅适用于本地目录不能与--remote、--stdout、--stdin、--split-output、--skill-generate或--copy组合使用。综合示例# 基础用法 repomix # 自定义输出 repomix -o output.xml --style xml # 输出到 stdout repomix --stdout custom-output.txt # 输出到 stdout 并管道给另一个命令例如 simonw/llm repomix --stdout | llm Please explain what this code does. # 压缩输出 repomix --compress # 处理特定文件 repomix --include src/**/*.ts --ignore **/*.test.ts # 拆分为多文件每部分大小上限 repomix --split-output 20mb # 远程仓库 分支 repomix --remote https://github.com/user/repo/tree/main # 远程仓库 commit repomix --remote https://github.com/user/repo/commit/836abcd7335137228ad77feb28655d85712680f1 # 远程仓库简写 repomix --remote user/repo # Watch 模式——文件变更自动重新打包 repomix --watch repomix -w --include src/**/*.ts相关资源输出格式详解 —— XML、Markdown、JSON、纯文本格式命令行选项全参考 —— 完整 CLI 参考提示词示例 —— 面向 AI 分析的示例提示词使用场景 —— 真实案例与工作流配置指南 —— 详细配置项说明代码压缩 —— 压缩机制细节注释移除 —— 支持的语言与细节安全 —— Repomix 检测内容说明通过以上命令与配置的组合你可以把任意规模的代码仓库本地目录、远程仓库或 stdin 精确选中的文件集合打包成最适合目标 AI 工具的单一文件同时借助 token 统计、代码压缩、Git 上下文与输出拆分把每次 AI 交互的信息密度和上下文质量做到最优。【免费下载链接】repomix Repomix is a powerful tool that packs your entire repository into a single, AI-friendly file. Perfect for when you need to feed your codebase to Large Language Models (LLMs) or other AI tools like Claude, ChatGPT, DeepSeek, Perplexity, Gemini, Gemma, Llama, Grok, and more.项目地址: https://gitcode.com/GitHub_Trending/rep/repomix创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考