git如何管理目录 [英] How does git manage directories

查看:99
本文介绍了git如何管理目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道git不会看到空的目录,但是有人可以提供一些关于如何实现它的文档的参考。
这不仅仅是空文件夹。如果我将一个文件添加到一个新文件夹中,但我没有将它添加到暂存区域,git实际上会看到该文件夹​​,但不会看到该文件。

I know git will not see am empty dir but can someone provide a reference to some documentation on how exactly it is implemented. It's not only about empty folders. If I add a file to a new folder, but I don't add it to the staging area, git actually sees the folder, but not the file.

推荐答案


我知道Git不会看到空的目录...

I know Git will not see an empty dir ...

这不太对。 Git将看到它就好,它不会保存

This is not quite right. Git will see it just fine, it just won't save it.


但有人可以提供一些关于如何实现它的文档的引用。

but can someone provide a reference to some documentation on how exactly it is implemented.

良好的软件通常会试图隐藏实现细节,暗示Git不是很好,:-)但在这种情况下,实现细节确实隐藏得很好。 Git的内部文档是 here ,其中一个框架 api-in-core-index.txt 最近9年前更新(!),以及更新的索引format.txt 。在任何情况下,跟踪都是关于Git的索引,它有几个名称:索引,暂存区域和缓存。

Good software usually tries to hide implementation details, which suggests that Git is not very good, :-) but in this case the implementation details really are pretty well hidden. Git's internals documentation is here, with one skeleton api-in-core-index.txt last updated 9 years ago (!), and a more recent index-format.txt. In any case, tracking is all about Git's index, which has several names: "the index", "the staging area", and "the cache".


这不仅仅是空文件夹。如果我将一个文件添加到一个新文件夹中,但是我没有将它添加到暂存区域,Git实际上会看到该文件夹​​,但不会看到该文件。

It's not only about empty folders. If I add a file to a new folder, but I don't add it to the staging area, Git actually sees the folder, but not the file.

这也不完全正确。尝试运行 git status -uall (或者等价地, git status --untracked-files = all )。 1 这里发生的事情是, git status 命令通常通过一个简单的汇总未跟踪的文件规则:如果存在名为 dir 的目录,并且在 dir 中找到了一些未跟踪的文件,但没有跟踪文件在 dir 中找到,Git只是打印 dir / ,而不是枚举每个文件 / em> dir

That's not quite right either. Try running git status -uall (or, equivalently, git status --untracked-files=all).1 What's happening here is that the git status command normally summarizes the untracked files via a simple rule: if a directory named dir exists, and some untracked files were found within dir but no tracked files were found within dir, Git just prints dir/ rather than enumerating each file within dir.

如果您使用 -uno (或 - untracked-files = no ),Git甚至不会查找未追踪的文件,这样可以节省时间。在一个大型资源库(数以万计的目录,数十万甚至数百万个文件)中,这可以使得 git status 之间的差异在一秒之内消失,而 git status 花费很多秒。

If you use -uno (or --untracked-files=no), Git does not even look for untracked files, which saves time. In a large repository (tens of thousands of directories, hundreds of thousands or even millions of files), this can make the difference between git status taking well under one second, and git status taking many seconds.

查找所有未跟踪文件需要比较实际工作量,树存储到索引中存储的工作树的缓存版本。使用正常的(总结)模式,Git有时可以使用它的缓存来避免不仅枚举 dir 内的文件,甚至还可以 dir ,这也节省了时间。

Finding all untracked files requires comparing the actual work-tree to the cached version of the work-tree stored in the index. With the normal (summarizing) mode, Git can sometimes use its cache to avoid not only enumerating the files within dir, but even looking inside dir, which also saves time.

当然,不寻找未跟踪的文件意味着Git永远不会提醒你 git add 这样的文件。所以默认的(总结)模式就是一种妥协方式,无论是在操作速度方面(如果 dir 本身包含任何文件 2 或者通过子目录,但我们已经知道 dir 中没有文件被跟踪,不必为文件做更细粒度的扫描)和可用性(当我们可以说 dir / )时,不需要在 dir 内的19,365个文件名发送垃圾邮件。

Of course, not looking for untracked file at all means Git will never remind you to git add such files. So the default (summarizing) mode is meant as a compromise, both in terms of speed of operation ("If dir contains any files2 in itself or via subdirectories, but we already know no files within dir are tracked, don't bother doing finer-grained scans for files") and usability ("no need to spam the listing with 19,365 file names within dir when we can just say dir/").

1 如果您指定no选项,默认值为 --unormal ,但如果指定 -u ,则表示 -uall 。但是,您也可以设置 status.showUntrackedFiles 配置变量来修改默认值。

1The default if you specify no options is -unormal, but if you specify -u, that means -uall. You can, however, also set the status.showUntrackedFiles configuration variable to modify the default.

2 测试这个( dir 或其子目录包含任何普通文件)部分取决于对中的 d_type http://pubs.opengroup.org/onlinepubs/7908799/xsh/readdir.html =nofollow noreferrer> readdir dirent 数据,这不是POSIX所需要的,但是很常见(这在所有现代Unix变体中都是可以找到的)。最近的Git版本也对索引格式有一个未跟踪的缓存扩展,详见同样的技术文档,它允许Git在 stat 数据没有改变的情况下跳过读取未跟踪的目录,使用<$ c $ c> mtime 字段 stat 结构

2Testing this ("does dir or its subdirectories contain any ordinary files") partly depends on support for the d_type field in readdir's dirent data, which is not required by POSIX but is common (it's certainly found on all modern Unix variants). Recent versions of Git also have an "untracked cache" extension to the index format, described in that same technical documentation, that allows Git to skip reading the untracked directories if their stat data have not changed, using the mtime field of the stat structure.

这篇关于git如何管理目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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