首页 > 解决方案 > 为什么带有路径的 git checkout 不会影响 Pro Git 中声明的索引?

问题描述

我正在研究 Pro Git 以了解如何reset工作checkout。我现在了解树,以及每个命令如何根据模式和是否指定路径影响每棵树。但有一件事让我很困惑。

Pro Git 指定使用git checkout路径时:

[git checkout] 就像 git reset [branch] file 一样,它会在提交时使用该文件更新索引,但它也会覆盖工作目录中的文件。”</p>

但是,在我的实验中,我无法重现这种预期的行为。

如果我在提交的主题分支redgreen,,,blue

9c070df (HEAD -> colors) blue
28a97c1 green
5edafd9 red

使用一个文件,其补丁是:

9c070df (HEAD -> colors) blue
diff --git a/colors.txt b/colors.txt
index 9d8beb6..ff67b54 100644
--- a/colors.txt
+++ b/colors.txt
@@ -1,2 +1,3 @@
 red
 green
+blue
28a97c1 green
diff --git a/colors.txt b/colors.txt
index a9d1386..9d8beb6 100644
--- a/colors.txt
+++ b/colors.txt
@@ -1 +1,2 @@
 red
+green
5edafd9 red
diff --git a/colors.txt b/colors.txt
new file mode 100644
index 0000000..a9d1386
--- /dev/null
+++ b/colors.txt
@@ -0,0 +1 @@
+red

如果HEAD是蓝色的,我git reset 5edafd9 -- colors.txt,我会有

+ green
+ blue

在工作树上,和

- green
- blue

在索引中,这是预期的,因为唯一的行red应用于索引。因此,当工作树与索引进行比较时,看起来像是添加了这些行,而当索引与头部进行了比较时,看起来这些行被删除了。这是预期和理解的。

但是当 I 时git checkout -- colors.txt,只有工作树受到影响,索引完好无损。

为什么是这样?

标签: git

解决方案



推荐阅读