为什么带有路径的git checkout不影响Pro Git中声明的索引? [英] Why doesn't git checkout with a path affect the index as declared in Pro Git?
问题描述
我正在研究Pro Git,以了解reset
和checkout
的工作方式.现在,我了解了这些树,以及每个命令如何影响每个树,具体取决于模式和是否指定了路径.但是有一件事让我感到困惑.
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指定在将git checkout
与路径一起使用时
Pro Git specifies that when using git checkout
with paths:
[git checkout]就像git reset [branch]文件一样,在该提交中,它使用该文件更新索引,但它也会覆盖工作目录中的文件." >
[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.
如果我正在提交red
,green
,blue
的主题分支:
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
如果HEAD
是蓝色,而我git reset 5edafd9 -- colors.txt
,我会拥有
If HEAD
is on blue, and I git reset 5edafd9 -- colors.txt
, I'll have
+ green
+ blue
在工作树上,然后
- green
- blue
索引中的
是预期的,因为只有red
行应用于索引.因此,当工作树与索引进行比较时,看起来好像添加了这些行,而索引与head进行了扩散时,看起来好像那些行已删除.这是意料之中的.
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.
但是当我git checkout -- colors.txt
时,仅工作树受到影响,索引保持不变.
But when I git checkout -- colors.txt
, only the working tree is affected, leaving the index intact.
这是为什么?
推荐答案
但是当我
git checkout -- colors.txt
时,仅工作树受到影响,索引保持不变
But when I
git checkout -- colors.txt
, only the working tree is affected, leaving the index intact
That is because this form of checkout (git checkout [<tree-ish>] [--] <pathspec>…
) is about overwriting paths in the working tree by replacing with the contents in the index or in the <tree-ish>
(most often a commit).
在Git 2.23中, git restore
将会做什么:
That will be, with Git 2.23, what git restore
will do:
'git restore
'默认情况下只会更新工作树. (而不是像git restore --staged --worktree --source
这样会同时更新工作树和索引的'checkout <tree> <paths>
').
'git restore
' by default will only update worktree. (as opposed to 'checkout <tree> <paths>
', which updates both worktree and index, like git restore --staged --worktree --source
would).
如果要切换分支,将使用 git switch
.
If you want to switch branch, you will use git switch
.
Git 2.23将在2019年8月的几天内发布.
Git 2.23 will be released in a few days, August 2019.
这篇关于为什么带有路径的git checkout不影响Pro Git中声明的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!