I'm studying Pro Git to understand how reset
and checkout
work. I understand now about the trees, and how each command affects each tree depending on the mode and if paths are specified. But one thing has me confused.
Pro Git specifies that when using git checkout
with paths:
[git checkout] is just like git reset [branch] file in that it updates the index with that file at that commit, but it also overwrites the file in the working directory.”
However, in my experimentation I'm unable to reproduce this expected behavior.
If I'm on a topic branch with commits red
, green
, blue
:
9c070df (HEAD -> colors) blue
28a97c1 green
5edafd9 red
with a single file whose patches are:
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
If HEAD
is on blue, and I git reset 5edafd9 -- colors.txt
, I'll have
+ green
+ blue
on the working tree, and
- green
- blue
in the index, which is expected, since the only line red
is applied to the index. So when working tree is diffed with the index, it looks like those lines are added, and when index is diffed with head, it looks like those lines are removed. That's expected and understood.
But when I git checkout -- colors.txt
, only the working tree is affected, leaving the index intact.
Why is this?