Git如何这么快地创建提交? [英] How does Git create commits so fast?

查看:73
本文介绍了Git如何这么快地创建提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,Git中的每个提交都是整个存储库的快照",这意味着至少必须读取每个文件.我的存储库为9.2 GB,一次提交花费了不到一秒钟的时间.怎么这么快都没道理.

From what I understand, each commit in Git is a "snapshot" of the entire repository, which means that, at the very least, every file has to be read. My repository is 9.2 GB and a commit takes a fraction of a second. Makes no sense how it happens so fast.

推荐答案

至少必须读取每个文件

at the very least, every file has to be read

相反,这是可能发生的最多.

On the contrary, that's the very most that could happen.

运行git commit来提交已分阶段的更改通常很快,因为实际上分阶段进行更改已完成了大部分工作.创建提交只需将索引(也称为暂存区")变成一个非常轻量级的提交对象,该对象包含有关提交的元数据以及一些树对象,其中包含存储库的结构.

Running git commit to commit your staged changes is generally fast because actually staging the changes did most of the work. Creating a commit simply turns the index (aka the "staging area") into a very lightweight commit object, which contains the metadata about your commit, and a few tree objects, which contain the structure of the repository.

但是,当您在特定文件上运行git add时,文件中的所有数据都会添加到git的数据库中.然后,有关该文件的数据将存储在暂存区域中,以便在运行git commit时,有关该文件的所有信息都已在索引中.因此,最昂贵的部分在运行git add时摊销.

All the data in the files, though, gets added to git's database when you run git add on a particular file. The data about that file is then stored in the staging area so that when you run git commit then all the information about that file is already in the index. So the costliest part is amortized over running git add.

另一个细微的事情是索引包含有关存储库中所有文件的信息-并且它维护有关工作目录的信息,例如上次检查文件及其文件的时间戳记尺寸.因此,即使您运行git add .之类的文件来暂存所有已更改的文件,也只需stat该文件即可确定文件是否已更改,如果未更改,则可以忽略它.

The other subtle thing is that the index contains the information about all the files in your repository - and it maintains information about the working directory like the time stamp that it last examined the file and its file size. So even if you run something like git add . to stage all the changed files, it only needs to stat the file to find out if it's changed, and it can ignore it if it hasn't.

显然,查看工作目录中的所有文件都有些昂贵,但是 的成本要比添加未更改文件的完整快照要便宜.

Obviously looking at all the files in your working directory is a little bit expensive, but much less costly than adding a full snapshot of even the unchanged files.

因此,即使git在每次提交时都存储了存储库的快照,它实际上只需要为已更改的文件存储新数据,它就可以为其他所有内容存储指向旧的,未更改的文件内容的指针.

So even though git stores a snapshot of the repository at each commit, it really only needs to store new data for the files that changed, it can store pointers to the old, unchanged file contents for everything else.

这篇关于Git如何这么快地创建提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆