我已经使用 git 一段时间了,但是当我在像 LibreOffice 这样的超大型项目中使用 git 时,我观察到了这一点。每当我第一次查询 git 时,它比使用相同命令的查询花费的时间要多得多:
~/Documents/libo : $ time git status
On branch task
nothing to commit, working directory clean
real 0m23.052s
user 0m0.328s
sys 0m1.248s
~/Documents/libo : $ time git status
On branch task
nothing to commit, working directory clean
real 0m0.415s
user 0m0.208s
sys 0m0.156s
~/Documents/libo : $
我的问题是:git 在其内部实现中是否使用某种缓存?如果是,那些缓存结果存储在 .git/* 目录中的什么位置?或者它与 git 无关还是取决于我使用的平台。
最佳答案
是也不是。
您显示的片段暗示您在某些 Unix-y 平台上,例如某些基于 GNU/Linux 或 *BSD 的操作系统或 Mac 操作系统。这些平台通常具有良好的文件系统缓存功能,因此 Git 下次扫描您的工作树时,许多/大部分信息将从主内存而不是磁盘提供。
另一方面,在 Windows 上,Git 想要执行的文件系统操作很慢,它的 ptalform 端口 Git for Windows 有一个由 core.fscache
configuration knob 控制的特殊功能。此功能实际上实现了工作树中文件的所谓“统计”信息的真正专用内存缓存。
据我所知,这个缓存在内存中,所以它没有存储在任何地方。
另外需要注意的是,所谓的“索引”——您为下一次提交暂存更改的特殊位置,以及由 git commit
创建的下一次提交被剪切的特殊位置实际上是一个特殊的缓存,在 Git 的早期就是这样命名的。一些 Git 命令仍然支持 --cached
命令行选项,这使得它们只考虑索引——引用 git help cli
手册:
NOTES ON FREQUENTLY CONFUSED OPTIONS
Many commands that can work on files in the working tree and/or in the index can take
--cached
and/or--index
options. Sometimes people incorrectly think that, because the index was originally called cache, these two are synonyms. They are not — these two options mean very different things.The
--cached
option is used to ask a command that usually works on files in the working tree to only work with the index. For example,git grep
, when used without a commit to specify from which commit to look for strings in, usually works on files in the working tree, but with the--cached
option, it looks for strings in the index.The
--index
option is used to ask a command that usually works on files in the working tree to also affect the index. For example, git stash apply usually merges changes recorded in a stash to the working tree, but with the--index
option, it also merges changes to the index as well.
git apply
command can be used with--cached
and--index
(but not at the same time). Usually the command only affects the files in the working tree, but with--index
, it patches both the files and their index entries, and with--cached
, it modifies only the index entries.
这种暂存区字面意思是“缓存”的情况源于
事实上,Git 最初被设想为一个所谓的实现
“内容可寻址文件系统”刚刚设法迅速超越
这个想法成为围绕该核心思想构建的成熟的 VCS。
缓存将保存将要记录为下一个文件系统的条目
用于快速访问的快照(提交)。即使在今天也是如此:指数
为 git status
工作保留暂存文件的“stat”信息
通过跳过实际计算文件的哈希值来快速计算似乎不是
与索引中的内容相比,工作树中的内容已发生更改。
请查看 Git SCM wiki 上的 "Git History" 页面并在此处查找“缓存”一词: 它很好地解释了该指数的历史背景。
最重要的是,这里有多种不同的缓存:操作系统的文件系统缓存、Git 自己的缓存以及启用时的 Windows 特定缓存。
实际上只有索引被“存储”:在没有特殊配置调整的普通 Git 中,这是位于“.git”子目录下的名为“index”的文件。
关于git - git 缓存它的结果吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41976793/