git 索引究竟包含什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4084921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:23:48  来源:igfitidea点击:

What does the git index contain EXACTLY?

gitinternal

提问by mochidino

What does the Git index exactly contain, and what command can I use to view the content of the index?

Git 索引究竟包含什么,我可以使用什么命令来查看索引的内容?



Update

更新

Thanks for all your answers. I know that the index acts as a staging area, and what is committed is in the index rather than the working tree. I am just curious about what an index object consists of. I guess it might be a list of filename/directory name, SHA-1 pairs, a kind of virtual tree maybe?

感谢您的所有回答。我知道索引充当临时区域,提交的内容在索引中而不是工作树中。我只是好奇索引对象由什么组成。我猜它可能是一个文件名/目录名列表、SHA-1 对、一种虚拟树?

Is there, in Git terminology, any plumbingcommand that I can use to list the contents of the index?

在 Git 术语中,是否有任何管道命令可用于列出索引的内容?

采纳答案by VonC

The Git book contains an article on what an index includes:

Git 书包含一篇关于索引包括什么的文章:

The index is a binary file (generally kept in .git/index) containing a sorted list of path names, each with permissions and the SHA1 of a blob object; git ls-filescan show you the contents of the index:

索引是一个二进制文件(通常保存在 中.git/index),其中包含路径名的排序列表,每个路径名都有权限和 blob 对象的 SHA1;git ls-files可以显示索引的内容:

$ git ls-files --stage
100644 63c918c667fa005ff12ad89437f2fdc80926e21c 0   .gitignore
100644 5529b198e8d14decbe4ad99db3f7fb632de0439d 0   .mailmap

The Racy git problemgives some more details on that structure:

情趣git的问题给出了该结构的详细说明:

The index is one of the most important data structures in git.
It represents a virtual working tree state by recording list of paths and their object names and serves as a staging area to write out the next tree object to be committed.
The state is "virtual" in the sense that it does not necessarily have to, and often does not, match the files in the working tree.

索引是 git 中最重要的数据结构之一。
它通过记录路径列表及其对象名称来表示虚拟工作树状态,并用作写出下一个要提交的树对象的暂存区。
从某种意义上说,状态是“虚拟的”,它不一定必须并且通常不匹配工作树中的文件。



To see more, cf. "git/git/Documentation/technical/index-format.txt":

要查看更多信息,请参见。“ git/git/Documentation/technical/index-format.txt”:

The Git index file has the following format

Git 索引文件具有以下格式

All binary numbers are in network byte order.
Version 2is described here unless stated otherwise.

  • A 12-byte header consisting of:
    • 4-byte signature:
      The signature is { 'D', 'I', 'R', 'C' } (stands for "dircache")
    • 4-byte version number:
      The current supported versions are 2, 3 and 4.
    • 32-bit number of index entries.
  • A number of sorted index entries.
  • Extensions:
    Extensions are identified by signature.
    Optional extensions can be ignored if Git does not understand them.
    Git currently supports cached tree and resolve undo extensions.
    • 4-byte extension signature. If the first byte is 'A'..'Z' the extension is optional and can be ignored.
    • 32-bit size of the extension
    • Extension data
  • 160-bit SHA-1 over the content of the index file before this checksum.

所有二进制数都按网络字节顺序排列。除非另有说明,此处描述了
版本 2

  • 一个 12 字节的标头,包括:
    • 4 字节签名
      签名为 { ' D', ' I', ' R', ' C' }(代表“ dircache”)
    • 4 字节版本号
      当前支持的版本为 2、3 和 4。
    • 32 位索引条目数。
  • 一些排序的索引条目
  • 扩展名
    扩展名由签名标识。
    如果 Git 不理解可选扩展,可以忽略它们。
    Git 目前支持缓存树和解析撤销扩展。
    • 4 字节扩展签名。如果第一个字节是 ' A'..' Z' 扩展名是可选的,可以忽略。
    • 扩展的 32 位大小
    • 扩展数据
  • 在此校验和之前对索引文件的内容进行 160 位 SHA-1。


mljrgcomments:

mljrg评论

If the index is the place where the next commit is prepared, why doesn't "git ls-files -s" return nothing after commit?

如果索引是准备下一次提交的地方,为什么“ git ls-files -s”提交后不返回任何内容?

Because the index represents what is being tracked, and right after a commit, what is being tracked is identical to the last commit (git diff --cachedreturns nothing).

因为索引代表正在跟踪的内容,并且在提交之后,正在跟踪的内容与上次提交相同(不git diff --cached返回任何内容)。

So git ls-files -slists all files tracked (object name, mode bits and stage number in the output).

因此git ls-files -s列出了所有跟踪的文件(输出中的对象名称、模式位和阶段编号)。

That list (of element tracked) is initialized with the content of a commit.
When you switch branch, the index content is reset to the commit referenced by the branch you just switched to.

该(被跟踪元素的)列表是用提交的内容初始化的。
当您切换分支时,索引内容将重置为您刚刚切换到的分支引用的提交。



Git 2.20 (Q4 2018) adds an Index Entry Offset Table (IEOT):

Git 2.20(2018 年第四季度)添加了索引条目偏移表 (IEOT)

See commit 77ff112, commit 3255089, commit abb4bb8, commit c780b9c, commit 3b1d9e0, commit 371ed0d(10 Oct 2018) by Ben Peart (benpeart).
See commit 252d079(26 Sep 2018) by Nguy?n Thái Ng?c Duy (pclouds).
(Merged by Junio C Hamano -- gitster--in commit e27bfaa, 19 Oct 2018)

参见Ben Peart ( ) 的commit 77ff112commit 3255089commit abb4bb8commit c780b9ccommit 3b1d9e0commit 371ed0d(2018 年 10 月 10 日。 请参阅Nguy?n Thái Ng?c Duy ( ) 的commit 252d079(2018 年 9 月 26 日(由Junio C Hamano合并-- --提交 e27bfaa 中,2018 年 10 月 19 日)benpeart
pclouds
gitster

ieot: add Index Entry Offset Table (IEOT) extension

This patch enables addressing the CPU cost of loading the index by adding additional data to the index that will allow us to efficiently multi- thread the loading and conversion of cache entries.

It accomplishes this by adding an (optional) index extension that is a table of offsets to blocks of cache entries in the index file.

To make this work for V4 indexes, when writing the cache entries, it periodically"resets" the prefix-compression by encoding the current entry as if the path name for the previous entry is completely different and saves the offset of that entry in the IEOT.
Basically, with V4 indexes, it generates offsets into blocks of prefix-compressed entries.

ieot:添加索引条目偏移表(IEOT)扩展

此补丁通过向索引添加额外数据来解决加载索引的 CPU 成本,这将使我们能够高效地多线程加载和转换缓存条目。

它通过添加一个(可选)索引扩展来实现这一点,该扩展是索引文件中缓存条目块的偏移量表。

为了使这适用于 V4 索引,在写入缓存条目时,它通过对当前条目进行编码来定期“重置”前缀压缩,就好像前一个条目的路径名完全不同,并将该条目的偏移量保存在 IEOT 中.
基本上,使用 V4 索引,它会在前缀压缩条目块中生成偏移量。

With the new index.threads config setting, the index loading is now faster.

使用新的 index.threads 配置设置,索引加载现在更快。



As a result (of using IEOT), commit 7bd9631clean-up the read-cache.c load_cache_entries_threaded()function for Git 2.23 (Q3 2019).

结果(使用 IEOT),提交 7bd9631清理read-cache.c load_cache_entries_threaded()Git 2.23(2019 年第三季度)的功能。

See commit 8373037, commit d713e88, commit d92349d, commit 113c29a, commit c95fc72, commit 7a2a721, commit c016579, commit be27fb7, commit 13a1781, commit 7bd9631, commit 3c1dce8, commit cf7a901, commit d64db5b, commit 76a7bc0(09 May 2019) by Jeff King (peff).
(Merged by Junio C Hamano -- gitster--in commit c0e78f7, 13 Jun 2019)

犯8373037提交d713e88提交d92349d提交113c29a提交c95fc72提交7a2a721提交c016579提交be27fb7提交13a1781提交7bd9631提交3c1dce8提交cf7a901提交d64db5b提交76a7bc0(2019年5月9日),由杰夫·金( peff)
(由Junio C gitsterHamano合并-- --提交 c0e78f7 中,2019 年 6 月 13 日)

read-cache: drop unused parameter from threaded load

The load_cache_entries_threaded()function takes a src_offsetparameter that it doesn't use. This has been there since its inception in 77ff112(read-cache: load cache entries on worker threads, 2018-10-10, Git v2.20.0-rc0).

Digging on the mailing list, that parameter was part of an earlier iteration of the series, but became unnecessary when the code switched to using the IEOT extension.

读取缓存:从线程加载中删除未使用的参数

load_cache_entries_threaded()函数采用src_offset它不使用的参数。自77ff112read-cache:在工作线程上加载缓存条目,2018-10-10,Git v2.20.0-rc0)以来,它就一直存在。

在邮件列表中挖掘,该参数是该系列早期迭代的一部分,但当代码切换到使用 IEOT 扩展时就变得不必要了。

回答by user225312

The Git index is a staging area between your working directory and your repository. You can use the index to build up a set of changes that you want to commit together. When you create a commit, what is committed is what is currently in this index, not what is in your working directory.

Git 索引是工作目录和存储库之间的临时区域。您可以使用索引来构建一组要一起提交的更改。创建提交时,提交的是当前在此索引中的内容,而不是您工作目录中的内容。

To see what is inside the index, issue the command:

要查看索引中的内容,请发出以下命令:

git status

When you run git status, you can see which files are staged (currently in your index), which are modified but not yet staged, and which are completely untracked.

当您运行 git status 时,您可以看到哪些文件已暂存(当前在您的索引中),哪些已修改但尚未暂存,以及哪些文件完全未跟踪。

You can read this. A Google search throws up many links, which should be fairly self sufficient.

你可以阅读这个。谷歌搜索会抛出许多链接,这应该是相当自给自足的。

回答by l h

Here is what you exactly needed, use this command.

这是您真正需要的,使用此命令。

$ binwalk index

$ binwalk 索引

DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
1717          0x6B5           Unix path: /company/user/user/delete.php
1813          0x715           Unix path: /company/user/user/get.php
1909          0x775           Unix path: /company/user/user/post.php
2005          0x7D5           Unix path: /company/user/user/put.php
3373          0xD2D           Unix path: /urban-airship/channel/channel/post.php
3789          0xECD           Unix path: /urban-airship/named-user/named-user/post.php
3901          0xF3D           Unix path: /user/categories/categories/delete.php
4005          0xFA5           Unix path: /user/categories/categories/get.php
4109          0x100D          Unix path: /user/categories/categories/put.php
4309          0x10D5          Unix path: /user/favorites/favorites/delete.php