简体   繁体   English

工作目录和 git 索引有什么区别?

[英]What is the difference between the working directory and the git Index?

The git book defines the git index: git 书定义了 git 索引:

The Git index is used as a staging area between your working directory and your repository. Git 索引用作工作目录和存储库之间的暂存区域。 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 the index, not what is in your working directory .创建提交时,提交的是当前索引中的内容,而不是工作目录中的内容

But I am still having a difficult time understanding it, especially the highlighted statement that "what's committed is not what's in my working directory".但是我仍然很难理解它,尤其是突出显示的“提交的内容不是我的工作目录中的内容”的声明。

So far, in my limited working with git, everything in the working directory is always committed, if I do:到目前为止,在我使用 git 的有限工作中,如果我这样做,工作目录中的所有内容都会始终提交:

git add <all new files in the working directory>
git commit -a -m "git will refuse to commit without this comment" 

git then commits all modified files as well as all new files. git 然后提交所有修改的文件以及所有新文件。

So, in effect, my working directory is the staging area?那么,实际上,我的工作目录暂存区吗?

I am not sure then what the git index is and how it is interpreted as the staging area.我不确定git index是什么以及它如何被解释为暂存区。

Could you please explain?你能解释一下吗?

The trick is:诀窍是:

when you add (git add) to the index, you don't have to commit right away当您将 (git add) 添加到索引时,您不必立即提交

So if you add some super complex function, and then proceed to change and... finally break it completely, you still can commit, because what is in your index (what you have added 10 minutes ago before break it with further failed modifications) is not what is currently in your working tree (which is hopelessly broken right now).因此,如果您添加一些超级复杂的 function,然后继续更改并...最终完全破坏它,您仍然可以提交,因为您的索引中有什么(您在 10 分钟前添加的内容,然后通过进一步失败的修改来破坏它)不是您当前工作树中的内容(现在已无可救药地损坏了)。

So it can help adding from time to time to the index a current development effort, knowing that you can commit at any time the last "stable" state you have indexed.因此,它可以帮助不时向索引添加当前的开发工作,因为您知道您可以随时提交您已索引的最后一个“稳定”state。


The other way what is committed is not what is in your working tree is when you git add --patch :另一种提交的方式不是你的工作树中的内容是当你git add --patch时:

Interactively choose hunks of patch between the index and the work tree and add them to the index.在索引和工作树之间以交互方式选择补丁块并将它们添加到索引中。
This gives the user a chance to review the difference before adding modified contents to the index.这使用户有机会在将修改的内容添加到索引之前查看差异。

You can add portion of your current file to the index (like one of the three functions you are writing), and then commit only that .您可以将当前文件的一部分添加到索引中(就像您正在编写的三个函数之一),然后只提交.

The index is a copy of the directory tree managed by git.该索引是由 git 管理的目录树的副本。 Initially, it is a copy of what is in the HEAD commit.最初,它是 HEAD 提交中内容的副本。 git add copies files from the working directory to the index. git add副本文件从工作目录到索引。 git commit creates a new commit from what is in the index. git commit从索引中的内容创建一个新提交。

The index is like a buffer-- it is not stored in the git history but access to it is controlled by git (unlike your working directory, which can be accessed in any number of ways).索引就像一个缓冲区——它不存储在 git 历史记录中,但对它的访问由 git 控制(与您的工作目录不同,它可以通过多种方式访问)。 git commits from the index so what is committed is something that git controls. git 从索引提交,所以提交的是 git 控制的内容。

The index/staging area is NOT your working directory.索引/暂存区不是您的工作目录。 You can do a simple test to see this.你可以做一个简单的测试来看看这个。 Create a file in your working directory called, say, foo .在您的工作目录中创建一个名为foo的文件。 Add some text to the file.在文件中添加一些文本。 Then do git add foo .然后做git add foo Now edit foo again and add (or remove) some more text.现在再次编辑foo并添加(或删除)更多文本。

If you run git diff --cached (which shows what's in the index), you'll only see foo as it was after the first round of edits and subsequent git add .如果您运行git diff --cached (显示索引中的内容),您只会看到foo在第一轮编辑和随后的git add之后的样子。 If you do git diff (which shows what's changed in your working directory), you will see all of the additional modifications you have made since the git add .如果您执行git diff (显示工作目录中的更改),您将看到自git add以来所做的所有其他修改。

The answer in your particular case is that you are understanding the documentation correctly, but using the "shortcut" command to commit your entire working directory.在您的特定情况下,答案是您正确理解了文档,但使用“快捷方式”命令提交整个工作目录。

If you run git commit -a -m "Message" then your working directory is treated like it is the staging area.如果您运行git commit -a -m "Message"那么您的工作目录将被视为暂存区。 This is convenient sometimes, but you lose the ability to use the index as designed.有时这很方便,但您失去了按设计使用索引的能力。 Try the following command:尝试以下命令:

git commit -m "Message"

If you do this instead, you can use the staging area to commit only part of the changes you have made to your working directory.如果您改为这样做,则可以使用暂存区域仅提交您对工作目录所做的部分更改。

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

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