# Mercurial

**Mercurial** 是一款自由、分布式的源代码管理工具，它为每位开发者提供项目完整开发历史的本地副本，因此提交、分支和合并都无需依赖中心服务器或网络连接即可进行。它以 Python、C 和 Rust 编写，采用 GNU 通用公共许可证第 2 版或更高版本授权，调用命令为 `hg`，即汞的化学元素符号。<sup>[1](https://mercurial-scm.org/about)</sup><sup> • </sup><sup>[2](https://www.mercurial-scm.org/install.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 性质 | 一款自由、分布式的源代码管理工具，采用 GPLv2 或更高版本授权<sup>[1](https://mercurial-scm.org/about)</sup> |
| 起源 | 由马特·麦克尔（Matt Mackall）于 2005 年 4 月 20 日在一封致 Linux 内核邮件列表的邮件中首次提出，作为专有软件 BitKeeper 的替代品<sup>[3](https://aosabook.org/en/mercurial.html)</sup><sup> • </sup><sup>[4](https://www.socallinuxexpo.org/sites/default/files/presentations/SCALE13x-2015-02-22-DVCS-Mercurial-v2%20%281%29.pdf)</sup> |
| 实现 | 以 Python 编写，性能关键部分用 C，且越来越多的部分用 Rust<sup>[2](https://www.mercurial-scm.org/install.html)</sup> |
| 存储模型 | 仅追加式修订日志（revlog），以增量加周期性快照的方式存储，分为文件日志、清单和变更日志<sup>[5](https://book.mercurial-scm.org/read/concepts.html)</sup> |
| 可扩展性 | 扩展是动态注入进程的 Python 文件，能够修改核心内部结构<sup>[6](https://book.mercurial-scm.org/read/app-git.html)</sup> |
| 当前版本 | 7.2.4，2026 年 8 月 11 日发布的缺陷修复版本；7.2 大版本于 2026 年 1 月 29 日发布<sup>[7](https://mercurial-scm.org/news/category/release)</sup> |
| 过去与现在的知名用户 | Mozilla 的 Firefox 仓库（直至 2025 年 4 月迁移至 Git）；Facebook/Meta，其 Sapling 系统最初是 Mercurial 的一个扩展<sup>[8](https://discourse.mozilla.org/t/engineering-effectiveness-newsletter-q2-2025-edition/144506)</sup><sup> • </sup><sup>[9](https://engineering.fb.com/2022/11/15/open-source/sapling-source-control-scalable/)</sup> |

## 历史

Mercurial 诞生于 2005 年 4 月，与 Git 同属那一波工具开发浪潮。当 Bitmover 宣布终止 BitKeeper 免费版——这款用于 [Linux 内核](https://www.edgepedia.org/zh/linux-kernel)开发的专有分布式系统——时，内核开发者们失去了版本控制工具，马特·麦克尔和[林纳斯·托瓦兹](https://www.edgepedia.org/zh/linus-torvalds)（Linus Torvalds）几乎同时在数日之内开始编写替代品。<sup>[3](https://aosabook.org/en/mercurial.html)</sup><sup> • </sup><sup>[10](https://www.socallinuxexpo.org/scale/13x/_assets/www.socallinuxexpo.org/sites/default/files/presentations/SCALE13x-2015-02-22-DVCS-Mercurial-v2%20%281%29.pdf)</sup> Mercurial 的最初构想，是麦克尔于 2005 年 4 月 20 日发给 Linux 内核邮件列表的一封邮件。<sup>[3](https://aosabook.org/en/mercurial.html)</sup>

早期版本所标榜的特性，至今仍定义着这个项目。2005 年 6 月 24 日发布的 0.6 版公告称，Mercurial 快速、易用、节省空间（将 Linux 内核自 2.4.0 以来的历史存于 300 MB 之内）、节省带宽（传输该历史仅需 120 MB）、分布式、可扩展且健壮，采用仅追加式存储模型，具备日志记录与回滚能力，而这一切都包含在一个小巧的 Python 代码库中。<sup>[11](https://lkml.rescloud.iu.edu/hypermail/linux/kernel/0506.3/0197.html)</sup> 1.0 版随后于 2008 年 3 月 24 日发布。<sup>[2](https://www.mercurial-scm.org/install.html)</sup>

## 架构与存储

Mercurial 的历史记录分为三层。每个受跟踪文件的历史存于一个文件日志中；清单为每个变更集汇集当时存在哪些文件、各处于哪个修订版；变更日志则记录每个变更集的作者、评论，以及指向其清单修订版的指针。每个修订版存储一个指向其直接父级的指针，合并时则指向两个父级。<sup>[5](https://book.mercurial-scm.org/read/concepts.html)</sup>

这三层都建立在一种名为修订日志的单一结构之上。修订日志不为每个修订版存储文件的完整副本，而是存储将旧修订版转换为新修订版所需的增量；对许多类型的数据而言，这些增量仅为完整副本体积的百分之零点几。与只能对文本做增量的旧系统不同，Mercurial 能处理任意二进制内容的增量。一旦自上次快照以来的累计增量信息超过固定阈值，修订日志便存储一个新的压缩快照，从而任何修订版都能被快速重建——这一做法后来被若干其他版本控制系统效仿。<sup>[5](https://book.mercurial-scm.org/read/concepts.html)</sup>

**构造上的可靠性。** Mercurial 只向修订日志文件末尾追加数据，从不修改已写入的部分，且每次写入都是一个原子事务的一部分：要么其全部效果一次性可见，要么整件事被撤销。写入者先完成文件日志和清单数据，再写变更日志数据，而读取者按相反顺序读取，因此读取者绝不会顺着指针读到写了一半的数据。读取完全不需要锁，因此任意数量的进程都可以在仓库被写入时读取它；写入则通过带可配置超时的锁来串行化。<sup>[5](https://book.mercurial-scm.org/read/concepts.html)</sup> 每个修订日志条目还携带其所代表数据的密码学哈希，既用作完整性校验，也用作使用者看到的修订版标识符。<sup>[5](https://book.mercurial-scm.org/read/concepts.html)</sup>

工作目录的状态记录在目录状态（dirstate）中，这是一个单一文件，记录工作目录更新到哪个变更集、跟踪哪些文件，以及每个文件的检出时间和大小，以便 `hg status` 能以最少的读取检测出修改。<sup>[5](https://book.mercurial-scm.org/read/concepts.html)</sup>

## 扩展与变更集演化

Mercurial 保持核心小巧，将额外行为推入扩展——扩展是动态注入运行中的 Mercurial 进程的 Python 文件。除了定义良好的扩展点之外，扩展几乎可以对 Mercurial 内部的一切进行猴子补丁，而无需创建新的发行版，项目将此视为与 Git 扩展模型的根本区别。许多核心功能最初都是先作为扩展证明自身价值的。<sup>[6](https://book.mercurial-scm.org/read/app-git.html)</sup> 随附的扩展包括用于访问控制钩子的 `acl`、用于事件日志的 `blackbox`、用于导入外来仓库的 `convert`，以及用于预生成克隆数据的 `clonebundles`。<sup>[12](https://mercurial-scm.org/help/extensions)</sup>

最重要的第三方扩展是 `evolve` 和 `topic`。变更集演化是 Mercurial 在完全分布式环境中安全协作处理草稿历史的基石。<sup>[13](https://mercurial-scm.org/news/2026/0005-london-sprint-recap)</sup> evolve 扩展启用变更集演化功能，提供修改历史及处理由此产生的问题的命令，并记录废弃标记，追踪被重写的变更集经历了什么。<sup>[14](https://mercurial-scm.org/help/extensions/evolve)</sup> 其 `hg evolve` 命令通过更新到后继版本、变基孤儿变更集、提取所需改动，或将分歧的变更集重新融合，来解决被归类为孤儿、阶段分歧或内容分歧的问题变更集。<sup>[15](https://mercurial-scm.org/help/commands/evolve)</sup> topic 扩展提供轻量级主题分支，一旦其变更集成为不可变历史的一部分便会消失；它仍属实验性功能，其命令可能变化。<sup>[16](https://mercurial-scm.org/help/extensions/topic)</sup>

## Mercurial 与 Git

Mercurial 和 Git 同年因同样的原因起步，在高层次上惊人地相似：两者都是分布式的，支持离线工作流和历史重写，并使用 SHA-1 内容哈希作为提交标识符。诸如 hg-git 之类的工具能在大多数情况下实现两种仓库格式之间的无损双向转换。<sup>[6](https://book.mercurial-scm.org/read/app-git.html)</sup> Git 的规范实现用 C 编写，Mercurial 的用 Python 编写，因此 Git 往往在原始性能比较中胜出——这是 Mercurial 为可扩展性而刻意做出的取舍。<sup>[6](https://book.mercurial-scm.org/read/app-git.html)</sup>

2025 年，一项结合合成仓库与随机森林分析的系统性研究认定，Git 在 CPU 和内存效率上优于 Mercurial，尤其在分支方面；而 Mercurial 提供更优的存储优化和一致性，使其非常适合面临存储限制的大型项目。<sup>[17](https://www.sciencedirect.com/science/article/pii/S2590005625001468)</sup> Mercurial 自身的开发也持续以性能为目标：在 7.2 版发布说明中，项目报告称存储修订版时的增量计算已提速，启用 Rust 扩展后某些基准测试从近 4 小时缩短到 2 分钟。<sup>[18](https://www.mercurial-scm.org/relnotes/7.2)</sup>

## 托管生态与采用情况

在实践中，两种工具的决定性差异在于托管，而非软件本身。[Bitbucket](https://www.edgepedia.org/zh/bitbucket) 于 2020 年 7 月 1 日弃用 Mercurial 功能并将其完全移除，援引 Stack Overflow 调查数据称 Mercurial 的开发者采用率约为 3%，而 Git 接近 90%，并指出 Bitbucket 新用户中选择 Mercurial 的不足 1%。<sup>[19](https://www.atlassian.com/blog/blog/bitbucket-blog/sunsetting-mercurial-support-in-bitbucket%29)</sup> 如今 Mercurial 托管主要依靠 [SourceForge](https://www.edgepedia.org/zh/sourceforge)、自托管，以及 Heptapod——一个由社区驱动、增加了 Mercurial 支持的 GitLab 分支，约于 2018 年由咨询公司 Octobus 创建，在 foss.heptapod.net 为自由及开源软件项目提供免费托管。<sup>[20](https://heptapod.net/heptapod/)</sup>

Mozilla 多年来在 hg.mozilla.org 上以 Mercurial 托管 Firefox 源代码，并于 2025 年 4 月 30 日将权威源迁移至 GitHub 上的 Git，该项目于 2023 年提出、同年 9 月获批；过渡期间 hg.mozilla.org 仍在服务，一个同步服务在两个系统之间映射提交。<sup>[8](https://discourse.mozilla.org/t/engineering-effectiveness-newsletter-q2-2025-edition/144506)</sup> Meta 的 Sapling 源代码管理系统最初是 Mercurial 开源项目的一个扩展，后来发展为拥有新存储格式、线协议和算法的独立系统；其客户端保留了 Mercurial 用户熟悉的概念，并能克隆 Git 仓库。<sup>[9](https://engineering.fb.com/2022/11/15/open-source/sapling-source-control-scalable/)</sup>

## 开发与发布

项目遵循大版本与小版本的发布节奏。Mercurial 7.0 于 2025 年 3 月 26 日发布，是首个符合 PEP 517 的版本；7.1 于 2025 年 8 月 4 日发布，是首个放弃 Python 3.8 支持的版本。<sup>[7](https://mercurial-scm.org/news/category/release)</sup> 2026 年 1 月 29 日的 7.2 大版本因开发者重写大量内部结构而推迟，它将 `dirstate-v2` 工作副本格式和 `diff.merge` 配置移出实验状态，引入了取代 `fncache` 格式的文件索引，并增加了对 Python 3.14 的支持。<sup>[7](https://mercurial-scm.org/news/category/release)</sup><sup> • </sup><sup>[18](https://www.mercurial-scm.org/relnotes/7.2)</sup> 代码库中越来越多的部分正为性能而用 Rust 编写，项目 2026 年的伦敦冲刺会讨论了一种修订日志格式的后继者，绰号 Hyperlog，旨在使变更集演化、窄克隆和发现成为一等功能，而非基于缓存。<sup>[2](https://www.mercurial-scm.org/install.html)</sup><sup> • </sup><sup>[13](https://mercurial-scm.org/news/2026/0005-london-sprint-recap)</sup>

## References

1. [About, mercurial-scm.org](https://mercurial-scm.org/about)
2. [Installation, mercurial-scm.org](https://www.mercurial-scm.org/install.html)
3. [Mercurial, The Architecture of Open Source Applications, Volume 1](https://aosabook.org/en/mercurial.html)
4. [Distributed Version Control presentation, SCALE 13x (2015)](https://www.socallinuxexpo.org/sites/default/files/presentations/SCALE13x-2015-02-22-DVCS-Mercurial-v2%20%281%29.pdf)
5. [Behind the scenes, Mercurial: The Definitive Guide](https://book.mercurial-scm.org/read/concepts.html)
6. [Appendix: Mercurial for Git Users, Mercurial: The Definitive Guide](https://book.mercurial-scm.org/read/app-git.html)
7. [Posts in release, mercurial-scm.org](https://mercurial-scm.org/news/category/release)
8. [Engineering Effectiveness Newsletter (Q2 2025 Edition), Mozilla Discourse](https://discourse.mozilla.org/t/engineering-effectiveness-newsletter-q2-2025-edition/144506)
9. [Sapling: Source control that's user-friendly and scalable, Meta Engineering](https://engineering.fb.com/2022/11/15/open-source/sapling-source-control-scalable/)
10. [git vs Mercurial (origins), SCALE 13x presentation](https://www.socallinuxexpo.org/scale/13x/_assets/www.socallinuxexpo.org/sites/default/files/presentations/SCALE13x-2015-02-22-DVCS-Mercurial-v2%20%281%29.pdf)
11. [Matt Mackall, "Mercurial 0.6 released!", Linux-Kernel Archive, June 24, 2005](https://lkml.rescloud.iu.edu/hypermail/linux/kernel/0506.3/0197.html)
12. [Extensions, Mercurial 7.2.4 documentation](https://mercurial-scm.org/help/extensions)
13. [Recapping the London sprint, mercurial-scm.org](https://mercurial-scm.org/news/2026/0005-london-sprint-recap)
14. [evolve extension, Mercurial documentation](https://mercurial-scm.org/help/extensions/evolve)
15. [hg evolve, Mercurial documentation](https://mercurial-scm.org/help/commands/evolve)
16. [topic extension, Mercurial documentation](https://mercurial-scm.org/help/extensions/topic)
17. [Feature-driven comparison of Git and Mercurial using DBSCAN and Random Forest, Array, Volume 28 (2025)](https://www.sciencedirect.com/science/article/pii/S2590005625001468)
18. [Mercurial 7.2 release notes (2026-01-29)](https://www.mercurial-scm.org/relnotes/7.2)
19. [Sunsetting Mercurial support in Bitbucket, Inside Atlassian](https://www.atlassian.com/blog/blog/bitbucket-blog/sunsetting-mercurial-support-in-bitbucket%29)
20. [About Heptapod](https://heptapod.net/heptapod/)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Development tools and collaboration infrastructure › Version control and collaboration platforms

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/mercurial

English original: https://www.edgepedia.org/mercurial
