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

发布于 2024-11-19 06:12:52 字数 677 浏览 2 评论 0原文

git book 定义了 git 索引:

Git 索引用作暂存 工作目录之间的区域 和你的存储库。您可以使用 建立一组变化的索引 你们想一起承诺。什么时候 你创建了一个提交,提交了什么 是索引中当前的内容,而不是 您的工作目录中有什么

但我仍然很难理解它,尤其是突出显示的“提交的内容不是我的工作目录中的内容”。

到目前为止,在我对 git 的有限使用中,工作目录中的所有内容总是提交,如果我这样做:

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

git然后提交所有修改的文件以及所有新文件。

那么,实际上,我的工作目录是暂存区?

我不确定 git index 是什么以及它如何被解释为暂存区域。

您能解释一下吗?

The git book defines the git index:

The Git index is used as 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 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 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.

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.

Could you please explain?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

紫竹語嫣☆ 2024-11-26 06:12:52

诀窍是:

当你添加(git add)到索引时,你不必立即提交

因此,如果你添加一些超级复杂的功能,然后继续更改......最后完全破坏它,你仍然可以提交,因为索引中的内容(在通过进一步失败的修改破坏它之前添加的内容)不是当前工作树中的内容(现在已经无可救药地破坏了)。

因此,它可以帮助不时向索引添加当前的开发工作,因为您知道您可以随时提交已索引的最后一个“稳定”状态。


另一种方式是,当您 git add --patch

在索引和工作树之间交互式选择补丁块并将它们添加到索引中。
这使用户有机会在将修改的内容添加到索引之前查看差异。

您可以将当前文件的部分添加到索引(就像您正在编写的三个函数之一),然后仅提交

The trick is:

when you add (git add) to the index, you don't have to commit right away

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).

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.


The other way what is committed is not what is in your working tree is when you 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.

漆黑的白昼 2024-11-26 06:12:52

索引是 git 管理的目录树的副本。最初,它是 HEAD 提交中内容的副本。 git add 将文件从工作目录复制到索引。 git commit 根据索引中的内容创建一个新的提交。

索引就像一个缓冲区——它不存储在 git 历史记录中,但对它的访问由 git 控制(与您的工作目录不同,可以通过多种方式访问​​它)。 git 从索引提交,所以提交的内容是 git 控制的。

The index is a copy of the directory tree managed by git. Initially, it is a copy of what is in the HEAD commit. git add copies files from the working directory to the index. git commit creates a new commit from what is in the index.

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 commits from the index so what is committed is something that git controls.

熟人话多 2024-11-26 06:12:52

在您的特定情况下,答案是您正确理解文档,但使用“快捷方式”命令提交整个工作目录。

如果您运行 git commit -a -m "Message" ,那么您的工作目录将被视为暂存区域。有时这很方便,但您会失去按设计使用索引的能力。尝试以下命令:

git commit -m "Message"

如果您这样做,则可以使用暂存区域仅提交对工作目录所做的部分更改。

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. 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.

寄与心 2024-11-26 06:12:52

索引/暂存区域不是您的工作目录。您可以做一个简单的测试来看看这一点。在工作目录中创建一个名为 foo 的文件。向文件添加一些文本。然后执行git add foo。现在再次编辑 foo 并添加(或删除)更多文本。

如果您运行 git diff --cached(显示索引中的内容),您只会看到第一轮编辑和后续编辑后的 ​​foo 内容>git 添加。如果您执行 git diff(显示工作目录中的更改),您将看到自 git add 以来所做的所有其他修改。

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. Add some text to the file. Then do git add foo. Now edit foo again and add (or remove) some more text.

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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文