简体   繁体   English

Git 暂存区只是一个索引吗?

[英]Is the Git staging area just an index?

The book Pro Git says that the staging area is just a list, or index, that says which files will be committed when a git commit is done, and now the name index is more commonly known as the "staging area". Pro Git 一书中说,staging area 只是一个列表或索引,它表示当git commit完成时将git commit哪些文件,现在名称index更常被称为“staging area”。

But if we modify the file foo.txt that is already part of the repo, and use git add foo.txt to stage it, and modify the file again, now the file is both "staged" and "modified" (as seen in git status ), and if we commit, the "staged" version will go into the commit.但是如果我们修改已经是 repo 一部分的foo.txt文件,并使用git add foo.txt将它git add foo.txt ,然后再次修改该文件,现在该文件既“暂存”又“修改”(如git status ),如果我们提交,“暂存”版本将进入提交。 The second edit won't go in.第二次编辑不会进去。

So how can the "staging area" keep track of what the first edit was if it is just an index -- a list of files?那么如果“暂存区”只是一个索引——一个文件列表,它如何跟踪第一次编辑的内容?

Index is a view of your working directory that is ready for commit.索引是准备提交的工作目录的视图。 It can be seen as a pre-commit state and is not as simple as a "list of files".它可以看作是一种预提交状态,并不像“文件列表”那么简单。 When you do git add , the file (with the change) is added to the index and the newer changes will not be see until you add them too.当您执行git add ,文件(带有更改)将添加到索引中,并且在您添加它们之前不会看到较新的更改。

The index is like an out basket of completed work.index就像一篮子已完成的工作。 At any point you can add a (part) completed file to that out basket and it will replace the previous copy with your current copy, so that when you finally decide to commit it will use the contents of that out basket (the current index ) to create the commit.在任何时候,您都可以add一个(部分)完成的文件add到该输出篮中,它将用您当前的副本替换之前的副本,以便当您最终决定commit ,它将使用该输出篮的内容(当前index )创建提交。

In addition your earlier add will have create a blob object within the repo that can be found if required via the various logs.此外,您之前的add将在 repo 中创建一个 blob 对象,如果需要,可以通过各种日志找到该对象。 After a while (30 days+) it will disappear with gc .一段时间后(30 天以上)它会随着gc消失。

It's an index but to a list of modification trees, not files directly.它是一个索引,但指向一个修改树列表,而不是直接指向文件。 See the different type of objects git handle .查看不同类型的对象 git handle

So how can the "staging area" keep track of what the first edit was if it is just an index -- a list?那么,如果“暂存区”只是一个索引——一个列表,那么它如何跟踪第一次编辑的内容?

An index is a list of names and pointers to content .索引是名称和指向内容的指针的列表。 In books, it's page numbers.在书籍中,它是页码。 In the Git index, it's object ID's in the repository's object database.在 Git 索引中,它是存储库对象数据库中的对象 ID。

That's what the Git index is, a pathname-indexed list of content pointers.这就是 Git 索引,内容指针的路径名索引列表。

git add for some pathname is basically某些路径名的git add基本上是

sha=`git hash-object -w path/to/it`
git update-index --cacheinfo 100644,$sha,path/to/it 

except git add checks for executable files and uses 100755 for those, and does recursive adds and checks your .gitignore and whatever else seems like it's usually most convenient.除了git add检查可执行文件并为这些文件使用100755 ,并且递归添加和检查您的.gitignore以及其他任何看起来通常最方便的东西。 It's a convenience command for adding content to the object db and updating the index.这是一个方便的命令,用于向对象数据库添加内容并更新索引。

The staging area is not just a list, nor index, which says which files will be committed when a git commit is done.集结区域只是一个列表,也没有指标,这表示文件将在一个git的承诺完成提交。

If it were that, ie a simple list, git add could never work as advertised.如果是这样,即一个简单的列表, git add永远不会像宣传的那样工作。

Rather, git add has to save the contents of the file at the time that the add command is given.相反, git add必须在给出 add 命令时保存文件的内容。 So it snapshots files, and then puts these snapshots into the staging area, (aka 'the index', which IMHO is really a rather poor choice for a name).因此它对文件进行快照,然后将这些快照放入暂存区(又名“索引”,恕我直言,这对于名称来说确实是一个相当糟糕的选择)。

So yes, in fact, the book's statement is misleading and confusing.所以是的,事实上,这本书的陈述具有误导性和混淆性。 But this isn't too surprising.但这并不太令人惊讶。 Much of the git documentation is confusing and poorly thought out.大部分 git 文档都很混乱,而且没有经过深思熟虑。

Go ahead and mark me down.来吧,标记我。 I'm sure I'm right about this.我确定我是对的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM