# Git

**Git** 是一个自由开源的分布式版本控制系统（DVCS），用于记录文件（最常见的是软件源代码）的变更，从而可以恢复项目的任何早期版本，并允许多个贡献者并行工作。Git 的核心是一个内容寻址的文件系统，其上构建了版本控制的用户界面。<sup>[1](https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain.html)</sup> 它由[林纳斯·托瓦兹](https://www.edgepedia.org/zh/linus-torvalds)（Linus Torvalds）于 2005 年为 [Linux 内核](https://www.edgepedia.org/zh/linux-kernel)开发而创建，目前由滨野纯（Junio C Hamano）维护。<sup>[2](https://tile.loc.gov/storage-services/master/gdc/gdcebookspublic/20/20/71/64/51/2020716451/2020716451.pdf)</sup><sup> • </sup><sup>[3](https://www.kernel.org/pub/software/scm/git/docs/git.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 创建者 | Linux 的创造者林纳斯·托瓦兹于 2005 年编写了 Git，此前用于内核开发的免费 BitKeeper 许可证被撤销<sup>[2](https://tile.loc.gov/storage-services/master/gdc/gdcebookspublic/20/20/71/64/51/2020716451/2020716451.pdf)</sup> |
| 首次发布 | 最初的版本由托瓦兹于 2005 年 4 月 7 日发布；开发工作从 2005 年 4 月 10 日起算<sup>[4](https://www.kernel.org/doc/mirror/ols2006v1.pdf)</sup><sup> • </sup><sup>[5](https://www.linuxfoundation.org/hubfs/Reports/2020_kernel_history_report_082720.pdf?hsLang=en)</sup> |
| 数据模型 | 以一系列快照而非变更集或差异来存储数据；每个提交指向一个快照树及其父提交<sup>[6](https://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell)</sup> |
| 分支 | 分支是一个轻量级的、可移动的、指向某个提交的指针；创建分支和切换分支几乎是瞬时的<sup>[6](https://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell)</sup> |
| 分布式 | 每个克隆都镜像了完整的仓库及其历史，因此每个克隆都是数据的完整备份<sup>[7](https://git-scm.com/book/en/v2/Getting-Started-About-Version-Control)</sup> |
| 维护 | 由滨野纯维护；开发和维护主要在 Git 邮件列表 git@vger.kernel.org 上进行<sup>[3](https://www.kernel.org/pub/software/scm/git/docs/git.html)</sup> |
| 哈希过渡 | 该项目于 2018 年底选择 SHA-256 作为 SHA-1 的继任者，新初始化仓库的默认哈希算法将从 sha1 改为 sha256<sup>[8](https://git-scm.com/docs/hash-function-transition)</sup><sup> • </sup><sup>[9](https://git-scm.com/docs/BreakingChanges)</sup> |

## 历史

Linux 内核项目在 2000 年代初期使用了专有的 BitKeeper 系统。2005 年，内核社区与开发 BitKeeper 的公司之间关系破裂，该工具的免费使用状态被撤销。这促使 Linux 开发社区——尤其是托瓦兹——基于使用 BitKeeper 时积累的经验教训来开发自己的工具。<sup>[2](https://tile.loc.gov/storage-services/master/gdc/gdcebookspublic/20/20/71/64/51/2020716451/2020716451.pdf)</sup> 内核使用分布式版本控制始于 2002 年初，当时托瓦兹首次开始使用 BitKeeper；分布式方法本身就使得内核开发立即加速。<sup>[10](https://cdn.kernel.org/doc/html/latest/process/7.AdvancedTopics.html)</sup>

Git 最初的版本于 2005 年 4 月 7 日发布，只有少数几个命令：初始化仓库、更新索引、创建树对象、创建提交对象、打印对象内容、将树读入索引，以及显示索引与工作树之间的差异。即使只有这有限的一组命令，它也能够托管自身。仅仅两周后，底层的“管道”层就已经包含了如今 Git 中的许多核心数据结构，正是这个版本产生了存储在 Git 中的 Linux 内核现代历史的第一个提交（v2.6.12-rc2）。<sup>[4](https://www.kernel.org/doc/mirror/ols2006v1.pdf)</sup> Git 是专门为支持 Linux 内核项目的工作流程而设计的，该项目的发展过程高度分布式，涉及频繁、反复的合并。<sup>[4](https://www.kernel.org/doc/mirror/ols2006v1.pdf)</sup>

内核社区迅速采纳了 Git：内核于 2005 年切换到 Git 进行开发，大多数内核开发者逐渐使用 Git 树。<sup>[11](https://www.kernel.org/doc/ols/2011/ols2011-masters.pdf)</sup> Linux 基金会的内核历史报告记录了切换到 Git 后每个发行版的变更集数量稳步增长。<sup>[5](https://www.linuxfoundation.org/hubfs/Reports/2020_kernel_history_report_082720.pdf?hsLang=en)</sup> 此后 Git 远远超出了内核的范围；GitHub 将其描述为最流行的分布式版本控制系统，被用于开源和商业软件开发。<sup>[12](https://docs.github.com/en/get-started/using-git/about-git)</sup>

## 对象数据库

Git 的核心是一个简单的键值数据存储：你将任何类型的内容插入仓库，Git 返回一个唯一的键，之后可以用它来检索该内容。<sup>[13](https://git-scm.com/book/be/v2/Git-Internals-Git-Objects)</sup> 键是哈希值。使用哈希函数来寻址内容使得完整性检查变得容易，因为损坏内容的哈希值与其名称不匹配，同时也使得对象查找变得快速。<sup>[8](https://git-scm.com/docs/hash-function-transition)</sup>

Git 不以一系列变更集或差异的形式存储数据，而是以一系列快照的形式存储。当你提交时，Git 存储一个提交对象，其中包含指向暂存内容快照的指针、作者姓名和电子邮件、提交信息以及指向父提交的指针：初始提交零个父提交，普通提交一个父提交，合并两个或多个分支时多个父提交。暂存文件时会为每个文件计算校验和，并将文件内容存储为数据块（blob）；树对象列出目录内容并将文件名映射到数据块。<sup>[6](https://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell)</sup>

Git 存储的所有内容都位于 `git init` 创建的 `.git` 目录中。四个条目构成了仓库的核心：`objects` 目录存储数据库的所有内容，`refs` 目录存储指向提交对象的指针（分支、标签、远程等），`HEAD` 文件指向当前检出的分支，`index` 文件保存暂存区信息。<sup>[1](https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain.html)</sup> 松散对象使用 zlib 压缩。随着时间的推移，Git 将许多松散对象打包成一个名为打包文件（packfile）的二进制文件，以节省空间并提高效率。<sup>[14](https://git-scm.com/book/en/v2/Git-Internals-Packfiles.html)</sup>

## 管道与瓷器

由于 Git 最初是版本控制系统的工具包而非精致的最终用户工具，它有两类子命令。用户友好的命令如 `checkout`、`branch` 和 `remote` 被称为“瓷器”（porcelain）；设计为以 UNIX 风格链式调用或从脚本中调用的底层命令被称为“管道”（plumbing）。许多管道命令并非供手动使用，而是作为新工具和自定义脚本的构建块。<sup>[1](https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain.html)</sup> 管道的例子包括 `hash-object`，它将数据存入对象数据库并返回其键；以及 `fsck`，它验证数据库中对象的连通性和有效性，并报告缺失、不可达或损坏的对象。<sup>[13](https://git-scm.com/book/be/v2/Git-Internals-Git-Objects)</sup><sup> • </sup><sup>[15](https://git-scm.com/docs/git-fsck.html)</sup>

在 Git 的早期，主要是在 1.5 版本之前，用户界面强调的是文件系统而非精致的版本控制系统，且更为复杂。此后界面得到了改进，但对早期界面难用的刻板印象仍然存在。<sup>[1](https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain.html)</sup>

## 分支与合并

Git 中的分支只是一个轻量级的、可移动的、指向某个提交的指针。Git 中的默认分支名是 `master`；`git init` 命令默认创建它，它在任何方面都不特殊，与其他分支完全一样。每次提交时，当前分支指针自动向前移动。由于分支操作几乎是瞬时的，且分支间的切换通常同样快速，Git 鼓励频繁分支和合并的工作流程，甚至一天内多次。<sup>[6](https://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell)</sup>

合并使用可配置的策略。拉取或合并一个分支的默认策略是 `ort`，这是一种三方合并算法，当存在多个共同祖先时，它会创建一个祖先的合并树作为合并的参考。在取自 Linux 2.6 内核开发历史的实际合并提交上进行测试，报告了更少的合并冲突且未导致错误合并，并且该策略能处理重命名。它的名字“Ostensibly Recursive's Twin”（表面上是递归的双胞胎）反映了它是作为先前默认算法 `recursive` 的替代品而编写的。<sup>[16](https://git-scm.com/docs/git-rebase)</sup>

`git rebase` 将一个分支的提交重放到另一个分支上，交互式变基可以重新排序、修改提交信息、合并、拆分或删除提交。文档警告说，对其他人已基于它开展工作的分支进行变基或任何其他形式的重写是个坏主意，因为下游的任何人将被迫手动修复他们的历史。<sup>[16](https://git-scm.com/docs/git-rebase)</sup>

## 分布式工作流

在分布式版本控制系统中，客户端不仅检出文件的最新快照，还完整镜像仓库，包括其完整历史。如果任何服务器宕机，任何客户端仓库都可以复制回服务器以恢复它，因此每个克隆都是所有数据的完整备份。分布式系统还能很好地处理多个远程仓库，允许同时以不同方式与不同团队协作，并支持集中式系统中不可能实现的工作流程，如层级模型。<sup>[7](https://git-scm.com/book/en/v2/Getting-Started-About-Version-Control)</sup>

Linux 内核流程在实践中展示了这一模型。子系统维护者各自管理自己版本的内核源代码树，通常使用 Git；当合并窗口打开时，顶层维护者要求托瓦兹从他们的仓库中拉取他们选定的补丁。维护者又可以从其他维护者那里拉取，形成一条被称为“信任链”的仓库链。<sup>[17](https://kernel.org/doc/html/v5.9/process/2.Process.html)</sup>

## 安全性与哈希过渡

Git 最初使用 SHA-1 命名所有对象。安全研究人员已经展示了实际的 SHA-1 碰撞，包括 2017 年 2 月 23 日的 SHAttered 攻击；Git v2.13.0 及更高版本默认转向了不受 SHAttered 影响的加固 SHA-1 实现，但 SHA-1 仍然脆弱。2018 年底，该项目选择 SHA-256 作为其继任哈希，一种仓库格式扩展允许仓库使用 SHA-256 命名对象，并在 SHA-256 和 SHA-1 名称之间建立双向映射，以便对象可以通过任一方式寻址。SHA-256 仓库无法被旧版本的 Git 读取。<sup>[8](https://git-scm.com/docs/hash-function-transition)</sup> 该项目的破坏性变更文档指出，新初始化仓库的默认哈希算法将从“sha1”改为“sha256”，生态系统支持 sha256 对象格式的准备程度是这一变更的重要要求。<sup>[9](https://git-scm.com/docs/BreakingChanges)</sup>

Git 还支持 GPG 签名的提交：`git commit -S` 对提交进行签名，`git verify-commit` 验证提交对象上的 GPG 签名。<sup>[18](https://git-scm.com/docs/git-commit)</sup><sup> • </sup><sup>[19](https://git-scm.com/docs/git-verify-commit)</sup> 对于通过 HTTP 的远程访问，Git 通过凭证助手获取用户名和密码，凭证助手是外部程序，从操作系统钥匙串等长期存储中获取或保存凭证；内置助手包括 `cache`（在内存中短暂保存凭证）和 `store`（在磁盘上无限期保存凭证）。<sup>[20](https://git-scm.com/docs/gitcredentials)</sup>

## References

1. [Git Internals - Plumbing and Porcelain, Pro Git 2nd Edition](https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain.html)
2. [A Short History of Git, Pro Git (Apress)](https://tile.loc.gov/storage-services/master/gdc/gdcebookspublic/20/20/71/64/51/2020716451/2020716451.pdf)
3. [git(1) manual page](https://www.kernel.org/pub/software/scm/git/docs/git.html)
4. [GIT—A Stupid Content Tracker, 2006 Linux Symposium](https://www.kernel.org/doc/mirror/ols2006v1.pdf)
5. [2020 Linux Kernel History Report, Linux Foundation](https://www.linuxfoundation.org/hubfs/Reports/2020_kernel_history_report_082720.pdf?hsLang=en)
6. [Git Branching - Branches in a Nutshell, Pro Git 2nd Edition](https://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell)
7. [Getting Started - About Version Control, Pro Git 2nd Edition](https://git-scm.com/book/en/v2/Getting-Started-About-Version-Control)
8. [hash-function-transition documentation](https://git-scm.com/docs/hash-function-transition)
9. [BreakingChanges documentation](https://git-scm.com/docs/BreakingChanges)
10. [Advanced topics, Linux Kernel documentation](https://cdn.kernel.org/doc/html/latest/process/7.AdvancedTopics.html)
11. [State of the kernel, 2011 Linux Symposium](https://www.kernel.org/doc/ols/2011/ols2011-masters.pdf)
12. [About Git, GitHub Docs](https://docs.github.com/en/get-started/using-git/about-git)
13. [Git Internals - Git Objects, Pro Git 2nd Edition](https://git-scm.com/book/be/v2/Git-Internals-Git-Objects)
14. [Git Internals - Packfiles, Pro Git 2nd Edition](https://git-scm.com/book/en/v2/Git-Internals-Packfiles.html)
15. [git-fsck documentation](https://git-scm.com/docs/git-fsck.html)
16. [git-rebase documentation](https://git-scm.com/docs/git-rebase)
17. [How the development process works, Linux Kernel documentation](https://kernel.org/doc/html/v5.9/process/2.Process.html)
18. [git-commit documentation](https://git-scm.com/docs/git-commit)
19. [git-verify-commit documentation](https://git-scm.com/docs/git-verify-commit)
20. [gitcredentials documentation](https://git-scm.com/docs/gitcredentials)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 17, 2026; 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/git

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