# GitHub 代码语料库

GitHub 代码语料库是指任何由公开 GitHub 仓库汇编而成、经过文件筛选、并打包供代码机器学习模型使用的大型训练数据集。这个词并不指某一个数据集，而是一类数据集：[BigCode](https://www.edgepedia.org/zh/bigcode) 的 [The Stack](https://www.edgepedia.org/zh/the-stack)（第 1 至第 3 版）、由其构建的 StarCoderData 混合集、CodeParrot 的 GitHub Code 数据集，以及 OpenAI 用于训练 [Codex](https://www.edgepedia.org/zh/codex-2021-model) 的未公开集合。<sup>[1](https://arxiv.org/html/2211.15533v1)</sup><sup> • </sup><sup>[2](https://www.bigcode-project.org/docs/about/the-stack/)</sup><sup> • </sup><sup>[3](https://huggingface.co/datasets/bigcode/starcoderdata)</sup><sup> • </sup><sup>[4](https://huggingface.co/datasets/bigcode/the-stack-v2)</sup>

| 关键事实 | 详情 |
|---|---|
| 规模轨迹 | The Stack 从 6.4 TB（v1.2，358 种语言）增长到覆盖 1.042 亿个仓库的 67.5 TB（v2），再增长到覆盖 2.24 亿个仓库的 113.7 TB（v3）<sup>[2](https://www.bigcode-project.org/docs/about/the-stack/)</sup><sup> • </sup><sup>[4](https://huggingface.co/datasets/bigcode/the-stack-v2)</sup><sup> • </sup><sup>[5](https://www.deeplearning.ai/the-batch/fresh-data-for-coding-models-hugging-face-updated-the-stack-the-largest-crawl-of-public-github-repositories)</sup> |
| 许可证过滤 | 仅选择宽松许可的文件，大约保留全许可证 GitHub 抓取量的 10%；对于 96.93% 缺乏许可证元数据的仓库，使用 ScanCode Toolkit 在文件级别检测许可证<sup>[1](https://arxiv.org/html/2211.15533v1)</sup><sup> • </sup><sup>[4](https://huggingface.co/datasets/bigcode/the-stack-v2)</sup> |
| 重复 | The Stack v1 宽松许可子集中 38.6% 的文件是近重复文件（占体量 53.7%）；v2 中约 40% 的宽松许可文件是重复文件；另一项研究发现 GitHub 代码中 70% 由克隆构成<sup>[1](https://arxiv.org/html/2211.15533v1)</sup><sup> • </sup><sup>[4](https://huggingface.co/datasets/bigcode/the-stack-v2)</sup><sup> • </sup><sup>[6](https://psycnet.apa.org/doi/10.1145/3133908)</sup> |
| 去重收益 | 近去重将 HumanEval 的 pass@100 从 27.21% 提升到 37.00%，将 MBPP 的 pass@100 从 44.99% 提升到 54.69%<sup>[1](https://arxiv.org/html/2211.15533v1)</sup> |
| 旗舰模型 | SantaCoder、StarCoder（约 2500 亿 token，覆盖 86 种语言）以及 StarCoder2 系列均在 The Stack 上训练；Codex 使用来自 5400 万个仓库的 179 GB Python 代码训练，未发布数据也未披露许可情况<sup>[7](https://arxiv.org/html/2402.19173v1)</sup><sup> • </sup><sup>[3](https://huggingface.co/datasets/bigcode/starcoderdata)</sup><sup> • </sup><sup>[1](https://arxiv.org/html/2211.15533v1)</sup> |
| 诉讼 | 2022 年 11 月针对 Copilot 的 DMCA 指控集体诉讼被驳回，第九巡回法院在上诉中维持驳回，裁定 Copilot 与 Codex 创作了新作品，而非从现有副本中移除版权管理信息<sup>[8](https://www.lit-ip.aoshearman.com/siteFiles/55545/7e2dc3b8595341349a5a3e1a7255ce12_Doe_v._Github.pdf)</sup> |
| 著佐权泄漏 | 对 5.14 亿个代码文件的扫描发现，训练数据集中有 3800 万个强著佐权许可代码的完全重复件，且所检查的每个数据集都存在许可证不一致<sup>[9](https://arxiv.org/html/2403.15230)</sup> |

## GitHub 代码语料库是什么

各语料库在范围和开放程度上有所不同。The Stack 最初是 3.1 TB 的发布版，包含 30 种编程语言的宽松许可源代码，并以 v1.2 维护为 6.4 TB、覆盖 358 种语言。<sup>[1](https://arxiv.org/html/2211.15533v1)</sup><sup> • </sup><sup>[2](https://www.bigcode-project.org/docs/about/the-stack/)</sup> The Stack v2 从[软件遗产](https://www.edgepedia.org/zh/software-heritage)档案图谱而非新的 GitHub 抓取重建语料库。闭源模型使用了可比数据但未公开：Codex 从 5400 万个公开仓库收集了 179 GB 的 [Python](https://www.edgepedia.org/zh/python-programming-language) 文件，但既未发布数据也未发布许可信息，CodeGen 则在私有 217.3 GB 宽松许可 Python 数据集上训练。<sup>[1](https://arxiv.org/html/2211.15533v1)</sup>

发布时，The Stack 的规模是 CodeParrot 的三倍多，后者是当时第二大已发布代码数据集；The Stack 覆盖 30 种语言，而 [AlphaCode](https://www.edgepedia.org/zh/alphacode) 和 PolyCoder 覆盖 12 种，CodeGen 覆盖 6 种。<sup>[1](https://arxiv.org/html/2211.15533v1)</sup>

## 从仓库到训练数据

**抓取。** The Stack v2 通过遍历日期为 2023-09-06 的软件遗产图谱快照汇编而成，使用截至 2023-09-14 的 GHArchive 元数据，从 1.042 亿个仓库获得 32.8 亿个唯一文件。<sup>[4](https://huggingface.co/datasets/bigcode/the-stack-v2)</sup> v3 抓取则下载每个仓库默认分支的单一快照，不含 git 历史，跳过超过 5 MB 的文件、二进制文件以及星标少于五的复刻；抓取总计 439 亿个文件。<sup>[5](https://www.deeplearning.ai/the-batch/fresh-data-for-coding-models-hugging-face-updated-the-stack-the-largest-crawl-of-public-github-repositories)</sup>

**许可。** 只有 3.07% 的仓库带有仓库级许可证元数据，因此 v2 流程运行 ScanCode Toolkit 在文件级别检测许可证，并保留宽松许可文件。<sup>[4](https://huggingface.co/datasets/bigcode/the-stack-v2)</sup> 源头的许可证检测并不完美：约 50% 的 GitHub 仓库未指定许可证，约 30% 指定 MIT 许可证，一项研究发现 GPL-3 许可证文本出现在 2,871 个不同文件名下。<sup>[10](https://shape-of-code.com/2024/06/02/obtaining-source-code-for-training-llms/)</sup>

**质量过滤与 PII 清洗。** StarCoder2 流程丢弃字母字符占比低于 25%、含有超过 1,000 字符的行、平均行长超过 100 字符、带有自动生成标记或含有大型编码块的文件。<sup>[5](https://www.deeplearning.ai/the-batch/fresh-data-for-coding-models-hugging-face-updated-the-stack-the-largest-crawl-of-public-github-repositories)</sup> 由于从训练后模型权重中移除特定信息仍是未解决的技术问题，BigCode 在训练前应用自动化 PII 删除。<sup>[11](https://book.the-turing-way.org/project-design/data-security/data-governance/bigcode-casestudy/)</sup> 在 v3 中，一个名为 StarPII 的模型发现电子邮件、密钥、姓名、密码和 IP 地址，并用占位符替换。<sup>[5](https://www.deeplearning.ai/the-batch/fresh-data-for-coding-models-hugging-face-updated-the-stack-the-largest-crawl-of-public-github-repositories)</sup>

**去重。** v2 流程遵循 [SantaCoder](https://www.edgepedia.org/zh/santacoder) 方法，使用 MinHash 和局部敏感哈希对 5-gram 进行处理，Jaccard 相似度阈值为 0.7，每个重复组保留一个文件，优先保留星标和复刻更多的仓库中的文件。<sup>[7](https://arxiv.org/html/2402.19173v1)</sup> 在 v3 中，由估计重叠至少 70% 的文件形成簇，保留星标最多的仓库中的文件，若星标数相同，则依次按复刻数、许可证宽松度和最早创建日期决定。<sup>[5](https://www.deeplearning.ai/the-batch/fresh-data-for-coding-models-hugging-face-updated-the-stack-the-largest-crawl-of-public-github-repositories)</sup>

## 数据一览

语料库在三年内增长了一个数量级以上。完整（去重前）数据集从 v1 的 6.4 TB 增长到 v2 的 67.5 TB，去重训练集从 2.9 TB 增长到 32.1 TB，训练 token 从约 2000 亿增长到约 9000 亿。<sup>[4](https://huggingface.co/datasets/bigcode/the-stack-v2)</sup> The Stack v3 包括 stack-v3-train（15.9 TB，约 4.9 万亿 token，713 种语言，1.73 亿个仓库，已去重并 PII 清洗）和 stack-v3-full（113.7 TB，770 种语言，2.24 亿个仓库），知识截止日期为 2025 年 8 月 7 日。<sup>[5](https://www.deeplearning.ai/the-batch/fresh-data-for-coding-models-hugging-face-updated-the-stack-the-largest-crawl-of-public-github-repositories)</sup>

语言分布严重偏斜。在 v1 宽松许可数据集中，总体量超过 55% 由四种最大语言占据：HTML 746 GB；[JavaScript](https://www.edgepedia.org/zh/javascript) 486 GB；Java 271 GB；C 222 GB。<sup>[1](https://arxiv.org/html/2211.15533v1)</sup> 许可加剧了偏斜：C 和 JavaScript 等广泛采用的语言相对于 Julia 和 Scala 等小众语言被过度代表，而 SQL、Batchfile 和 [TypeScript](https://www.edgepedia.org/zh/typescript) 更不可能获得宽松许可（4% 对平均 10%），使其代表性偏斜，且宽松许可文件往往更长。<sup>[4](https://huggingface.co/datasets/bigcode/the-stack-v2)</sup>

## 在基础模型中的使用

BigCode 是由 [Hugging Face](https://www.edgepedia.org/zh/hugging-face) 和 [ServiceNow](https://www.edgepedia.org/zh/servicenow) 领导的合作项目，训练了 SantaCoder（一个 11 亿参数模型，使用 The Stack v1 中的 Java、JavaScript 和 Python），随后于 2023 年 5 月 4 日发布 [StarCoder](https://www.edgepedia.org/zh/starcoder)，并在 The Stack v2 上训练 StarCoder2 系列；Nvidia 和 JetBrains 也在早期版本上训练了模型。<sup>[7](https://arxiv.org/html/2402.19173v1)</sup><sup> • </sup><sup>[5](https://www.deeplearning.ai/the-batch/fresh-data-for-coding-models-hugging-face-updated-the-stack-the-largest-crawl-of-public-github-repositories)</sup> StarCoder 的训练混合集 StarCoderData 包含 86 种编程语言的 783 GB 代码，加上 54 GB 的 GitHub 议题、13 GB 的 Jupyter 笔记本和 32 GB 的 GitHub 提交记录，约 2500 亿 token，相对于 The Stack v1 进行了去污染、近去重和 PII 删除。<sup>[3](https://huggingface.co/datasets/bigcode/starcoderdata)</sup> 相比之下，Codex 使用 GitHub 公开仓库的代码训练，未发布其数据。<sup>[12](https://dl.acm.org/doi/fullHtml/10.1145/3597151)</sup><sup> • </sup><sup>[1](https://arxiv.org/html/2211.15533v1)</sup>

去重并非只是表面功夫。在近去重的 v1 子集上训练，将 [HumanEval](https://www.edgepedia.org/zh/humaneval) 的 pass@100 从 27.21% 提升到 37.00%，将 MBPP 的 pass@100 从 44.99% 提升到 54.69%。<sup>[1](https://arxiv.org/html/2211.15533v1)</sup>

## 与其他代码语料库的比较

在已发布数据集中，The Stack 的显著特征是规模、语言覆盖和逐文件来源。CodeParrot 是第二大已发布数据集，规模不到 The Stack 的三分之一，AlphaCode、PolyCoder 和 CodeGen 覆盖的语言更少。<sup>[1](https://arxiv.org/html/2211.15533v1)</sup>

## 许可、同意与争议

**Copilot 争议。** GitHub 于 2019 年开始开发 Copilot，2021 年 6 月有限发布，2022 年 6 月作为付费订阅发布；OpenAI 于 2021 年 8 月有限发布 Codex。<sup>[13](https://www.githubcopilotlitigation.com/pdf/06823/1-0-github_complaint.pdf)</sup> Copilot 使用从网络抓取的公开仓库训练，其中许多以要求复用时署名的许可证发布，随后于 2022 年 11 月发生集体诉讼。<sup>[14](https://www.theverge.com/2022/11/8/23446821/microsoft-openai-github-copilot-class-action-lawsuit-ai-copyright-violation-training-data)</sup> 争议围绕两大开源许可证家族：宽松许可和著佐权。<sup>[15](https://arxiv.org/pdf/2302.13681v2.pdf)</sup> GitHub 首席执行官纳特·弗里德曼（Nat Friedman）在 2021 年 6 月 29 日公告后公开声称公司在法律上没有问题；[自由软件基金会](https://www.edgepedia.org/zh/free-software-foundation)和原告主张，使用公开仓库训练可能违反开源许可证，这一分歧在公开记录中仍未解决。<sup>[16](https://www.theverge.com/2021/7/7/22561180/github-copilot-legal-copyright-fair-use-public-code)</sup><sup> • </sup><sup>[17](https://www.fsf.org/licensing/copilot/copyright-implications-of-the-use-of-code-repositories-to-train-a-machine-learning-model)</sup>

**法院裁定。** 第九巡回法院维持驳回原告的 DMCA 指控，裁定根据原告的输出理论，Copilot 和 Codex 并未从现有作品的副本中移除或更改版权管理信息，而是创作了从未包含该信息的新作品；合议庭确实认定原告具有第三条诉讼资格，因为他们合理主张了实质性伤害风险。<sup>[8](https://www.lit-ip.aoshearman.com/siteFiles/55545/7e2dc3b8595341349a5a3e1a7255ce12_Doe_v._Github.pdf)</sup> 截至 2024 年中，该案尚未进入陪审团审理。<sup>[10](https://shape-of-code.com/2024/06/02/obtaining-source-code-for-training-llms/)</sup>

**逐字输出与署名。** GitHub 表示，Copilot 的建议大约 0.1% 的情况下可能包含训练集中的逐字片段；FSF 指出这一数字不包括近逐字复制，例如仅更改变量名。<sup>[17](https://www.fsf.org/licensing/copilot/copyright-implications-of-the-use-of-code-repositories-to-train-a-machine-learning-model)</sup> 独立测量支持这一担忧：StarCoder 生成的前 100 个输出中 81% 和 InCoder 输出的 75% 被发现直接复制自 GitHub。<sup>[9](https://arxiv.org/html/2403.15230)</sup> Codex 基本逐字复现了 CSparse 函数 cs_transpose，未署名、未注明版权或许可证，GitHub 宣布计划于 2023 年提供一种机制，在 Copilot 输出近匹配代码时提供署名和许可证链接。<sup>[18](https://www.siam.org/publications/siam-news/articles/ethical-concerns-of-code-generation-through-artificial-intelligence/)</sup>

**BigCode 的治理制度。** The Stack 附带“我是否在 The Stack 中”搜索工具和通过 GitHub 仓库的退出机制；项目计划大约每三个月更新数据集，并要求下载者使用最新的允许版本，以便删除请求传播。<sup>[2](https://www.bigcode-project.org/docs/about/the-stack/)</sup> BigCode 表示，The Stack 中代码的用户必须遵守代码的原始许可证条款，包括署名条款，这由每个数据点的来源信息提供便利。<sup>[2](https://www.bigcode-project.org/docs/about/the-stack/)</sup> 与 Copilot 不同，BigCode 表示从其训练数据中排除著佐权代码，尽管开发者只能移除自己的仓库。<sup>[18](https://www.siam.org/publications/siam-news/articles/ethical-concerns-of-code-generation-through-artificial-intelligence/)</sup>

## 质量、重复与安全风险

对 450 万个非复刻 GitHub 项目、超过 4.28 亿个 Java、C++、Python 和 JavaScript 文件的分析发现只有 8500 万个唯一文件，这意味着 GitHub 上 70% 的代码由克隆构成；JavaScript 重复率最高，仅 6% 的文件独特，Java 最低，60% 独特，9% 到 31% 的项目包含至少 80% 在别处发现的文件。<sup>[6](https://psycnet.apa.org/doi/10.1145/3133908)</sup> 在训练语料库本身中，v1 宽松许可文件的 38.6% 和 v2 宽松许可文件的约 40% 是近重复件。<sup>[1](https://arxiv.org/html/2211.15533v1)</sup><sup> • </sup><sup>[4](https://huggingface.co/datasets/bigcode/the-stack-v2)</sup> 数据集卡警告，剩余近重复件可能导致泄漏到自定义测试分割中，早期工作已确立仅移除完全匹配和复刻是不够的；应在分割语料库之前应用去重。<sup>[4](https://huggingface.co/datasets/bigcode/the-stack-v2)</sup><sup> • </sup><sup>[19](https://arxiv.org/html/1812.06469v6)</sup>

尽管有过滤，许可证污染仍然存在。一项对 53 个语言模型训练数据集中 5.14 亿个代码文件的研究发现 3800 万个强著佐权许可代码的完全重复件；The Stack v1 包含 262,678,972 个文件，其中 16,122,976 个（6.14%）是强著佐权代码的完全重复件，[The Pile](https://www.edgepedia.org/zh/the-pile) 的重复率为 22.80%，即使按宽松许可标准整理的数据集也显示与 GPL 2.0、GPL 3.0 和 AGPL 代码的抓取数据集至少 5% 的重叠。<sup>[9](https://arxiv.org/html/2403.15230)</sup>

数据投毒也可以作为一种防御：CoProtector 系统让权利人对自己发布的代码投毒，以阻止未经授权的训练，仅 10% 投毒实例即可将代码模型准确率降低 7.3%，并在 0.1% 或 1% 投毒比例下产生可在 500 次用户查询内验证的水印。<sup>[20](https://arxiv.org/html/2110.12925v2)</sup>

## 近期发展

**退出执行。** 开发者须在 2023 年 11 月 20 日前提交 The Stack v2 的退出请求；截止后，与 91 个用户和组织相关的 1,561 个仓库被消除，22,066 个文件被移除。<sup>[7](https://arxiv.org/html/2402.19173v1)</sup> 版本历史显示该机制持续运作：v2.0.1（StarCoder2 在其上训练）移除了 2023-10-20 前退出的仓库，v2.1.0 移除了 2024-04-09 前退出的，v2.2.0 移除了 2026-07-29 前退出的以及不再在 GitHub 上的用户和组织的仓库。<sup>[4](https://huggingface.co/datasets/bigcode/the-stack-v2)</sup>

**更正与重新许可。** 在 v3 去重流程工作期间，团队发现一个错误导致 The Stack v2 丢弃了比计划更多的文件；v3 的方法修正了这一点。<sup>[5](https://www.deeplearning.ai/the-batch/fresh-data-for-coding-models-hugging-face-updated-the-stack-the-largest-crawl-of-public-github-repositories)</sup> The Stack v3 以开放数据共享署名许可证 v1.0 发布，允许商业和非商业使用并署名。<sup>[5](https://www.deeplearning.ai/the-batch/fresh-data-for-coding-models-hugging-face-updated-the-stack-the-largest-crawl-of-public-github-repositories)</sup>

## References

1. [The Stack: 3 TB of permissively licensed source code (BigCode, arXiv)](https://arxiv.org/html/2211.15533v1)
2. [The Stack, BigCode Project documentation](https://www.bigcode-project.org/docs/about/the-stack/)
3. [bigcode/starcoderdata dataset card, Hugging Face](https://huggingface.co/datasets/bigcode/starcoderdata)
4. [bigcode/the-stack-v2 dataset card, Hugging Face](https://huggingface.co/datasets/bigcode/the-stack-v2)
5. [Fresh Data for Coding Models: Hugging Face Updated The Stack, DeepLearning.AI The Batch](https://www.deeplearning.ai/the-batch/fresh-data-for-coding-models-hugging-face-updated-the-stack-the-largest-crawl-of-public-github-repositories)
6. [DéjàVu: a map of code duplicates on GitHub (PACMPL/OOPSLA)](https://psycnet.apa.org/doi/10.1145/3133908)
7. [StarCoder 2 and The Stack v2: The Next Generation (arXiv)](https://arxiv.org/html/2402.19173v1)
8. [Doe v. GitHub, Ninth Circuit opinion](https://www.lit-ip.aoshearman.com/siteFiles/55545/7e2dc3b8595341349a5a3e1a7255ce12_Doe_v._Github.pdf)
9. [An Exploratory Investigation into Code License Infringements in Large Language Model Training Datasets (arXiv)](https://arxiv.org/html/2403.15230)
10. [Obtaining source code for training LLMs, The Shape of Code](https://shape-of-code.com/2024/06/02/obtaining-source-code-for-training-llms/)
11. [BigCode Data Governance Case Study, The Turing Way](https://book.the-turing-way.org/project-design/data-security/data-governance/bigcode-casestudy/)
12. [Communications of the ACM article on Codex/Copilot training](https://dl.acm.org/doi/fullHtml/10.1145/3597151)
13. [Doe v. GitHub, Inc., class action complaint, November 2, 2022](https://www.githubcopilotlitigation.com/pdf/06823/1-0-github_complaint.pdf)
14. [The lawsuit against Microsoft, GitHub and OpenAI that could change the rules of AI copyright, The Verge](https://www.theverge.com/2022/11/8/23446821/microsoft-openai-github-copilot-class-action-lawsuit-ai-copyright-violation-training-data)
15. [The (ab)use of Open Source Code to Train Large Language Models (arXiv)](https://arxiv.org/pdf/2302.13681v2.pdf)
16. [GitHub's automatic coding tool rests on untested legal ground, The Verge](https://www.theverge.com/2021/7/7/22561180/github-copilot-legal-copyright-fair-use-public-code)
17. [Copyright Implications of the Use of Code Repositories to Train a Machine Learning Model, Free Software Foundation](https://www.fsf.org/licensing/copilot/copyright-implications-of-the-use-of-code-repositories-to-train-a-machine-learning-model)
18. [Ethical Concerns of Code Generation Through Artificial Intelligence, SIAM News](https://www.siam.org/publications/siam-news/articles/ethical-concerns-of-code-generation-through-artificial-intelligence/)
19. [The Adverse Effects of Code Duplication in Machine Learning Models of Code (arXiv)](https://arxiv.org/html/1812.06469v6)
20. [CoProtector: Protect Open-Source Code against Unauthorized Training Usage with Data Poisoning (arXiv)](https://arxiv.org/html/2110.12925v2)

---
*Initially written Oct 15, 2026 · Edited: Oct 25, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 现代人工智能：基础模型、生成式人工智能与人工智能产业 › Foundation-model methods and training › Pretraining data and corpora

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/github-code-corpus

English original: https://www.edgepedia.org/github-code-corpus
