git如何在内部跟踪文件更改? [英] How does git track file changes internally?

查看:136
本文介绍了git如何在内部跟踪文件更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释git在内部如何知道文件X,Y和Z已经改变了吗?什么是幕后过程,可以识别文件尚未添加或修改的时间?我在问,因为在Subversion中,通过在每个文件夹下有一个 .svn 目录来记录这些事情是很简单的,但是对于git我看起来似乎无法找到这个内部工作的描述。我怀疑它扫描了所有的子目录,因为它很快。



所以,如果好奇心,它的内部工作是什么?

解决方案

确定文件状态的机制相当简单。要知道哪些文件已经上演,只需将 HEAD 树与索引区分开来。任何只出现在索引中的项目都已经进行了添加,任何仅出现在 HEAD 中的项目已被删除,并且任何不同的项目都进行了更改。 p>

同样,通过将索引与工作目录区分开来,您可以检测未分类的更改。

问这个速度如何如此之快(毕竟,计算文件的SHA1哈希值并不是很快)。这就是索引 - 也称为 cache - 再次播放的地方。该索引还包含文件大小的字段和文件修改时间。因此,你可以简单地将 stat(2)文件放在磁盘上,并与索引的文件大小和文件修改时间进行比较,以确定是否对文件进行散列。


Could somebody explain how git knows internally that files X, Y and Z have changed? What is the process behind the scenes that recognizes when a file has not yet been added or has modifications? I am asking because, with Subversion it's simple to figure out that it keeps track of these things by having a .svn directory under each folder, but for git I can't seem to find a description of the inner workings of this. I doubt it scans through all the sub-directories for changes, as it's quite fast.

So, out if curiosity, what are it's inner workings?

解决方案

The mechanisms by which one determines the status of a file is fairly straightforward. To know what files have been staged, one simply diffs the HEAD tree with the index. Any items that appear only in the index have been staged for addition, any items that appear only in HEAD have been removed and any items that are different have had changes staged.

Similarly, one would detect unstaged changes by diff'ing the index with the working directory.

Your question in particular asks how this can be so fast (after all, computing the SHA1 hash of a file is not exactly speedy.) This is where the index - also known as the cache - comes in to play again. The index also has fields for the file size and file modification time. Thus one can simply stat(2) a file on disk and compare against the index's file size and file modification time to know whether to hash the file or not.

这篇关于git如何在内部跟踪文件更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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