运行git reset以更新索引条目后,可以使用git-checkout [1]检查索引中的内容到工作树。或者,使用git-checkout [1]并指定提交,您可以一次性将提交中的路径内容复制到索引和工作树。
问:有人能举例说明所引用段落的含义吗?
注意此问题特别要求引用git reset [-q] [<tree-ish>] [--] <paths>…
变体的段落。我知道git reset <path>
做了什么,但无法理解上面的代码片段
从git reset
开始,以获得更好的背景。假设我们在工作树中更改了hello.c,并将其添加到索引中。
$ git status
On branch feature02
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: hello.c
现在,运行git reset以将索引条目更新回状态,就像我们将其添加到索引之前一样。
$ git reset -- hello.c
Unstaged changes after reset:
M hello.c
$ git status
On branch feature02
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: hello.c
no changes added to commit (use "git add" and/or "git commit -a")
到目前为止,工作树仍然包含我们的最后更改,而索引更新以匹配HEAD。好吧,让我们使用git-checkout [1]检查索引到工作树的内容。请小心,因为此命令会丢失对工作树中hello.c的更改。
$ git checkout -- hello.c
$ git status
On branch feature02
nothing to commit, working tree clean
现在,索引和工作树都会更新以匹配HEAD。所以它说working tree clean
。
我们开始时回到原始状态:
$ git status
On branch feature02
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: hello.c
使用git-checkout [1]并指定提交。
$ git checkout HEAD hello.c
$ git status
On branch feature02
nothing to commit, working tree clean
我们在这里,索引和工作树都被更新以匹配HEAD。这就是为什么我们一气呵成。
这里的一些技巧是git checkout
的2种用法:
git checkout -- hello.c
&
git checkout HEAD hello.c
没有命名提交的第一个是从索引(到工作树)复制内容,而后者是从提交历史(到索引和工作树)。
我建议阅读Pro Git的前三章,这将使您更好地理解Git模型,这对理解这一点非常关键。
tl; dr是这个版本的reset
只更新索引(即缓存,临时区域)而不触及工作树(你可以看到的文件)或HEAD
,如果你这样做,你可以使用checkout
带来你的与索引一致的工作树(或者只是使用checkout
来执行这两个操作 - 获取特定版本的文件并同时使用它们更新索引和工作树)。
我们以Git存储库为例。首先,我将确定一个特定的提交和一个特定的更改用作示例:
$ git show --stat HEAD
commit e7e07d5a4fcc2a203d9873968ad3e6bd4d7419d7
Author: Junio C Hamano <[email protected]>
Date: Fri Feb 24 10:49:58 2017 -0800
Git 2.12
Signed-off-by: Junio C Hamano <[email protected]>
Documentation/RelNotes/2.12.0.txt | 6 ++++++
Documentation/git.txt | 5 +++++
GIT-VERSION-GEN | 2 +-
3 files changed, 12 insertions(+), 1 deletion(-)
$ git show HEAD -- Documentation/git.txt
commit e7e07d5a4fcc2a203d9873968ad3e6bd4d7419d7
Author: Junio C Hamano <[email protected]>
Date: Fri Feb 24 10:49:58 2017 -0800
Git 2.12
Signed-off-by: Junio C Hamano <[email protected]>
diff --git a/Documentation/git.txt b/Documentation/git.txt
index 4f208fab9..aa895da4a 100644
--- a/Documentation/git.txt
+++ b/Documentation/git.txt
@@ -44,6 +44,11 @@ unreleased) version of Git, that is available from the 'master'
branch of the `git.git` repository.
Documentation for older releases are available here:
+* link:v2.12.0/git.html[documentation for release 2.12.0]
+
+* release notes for
+ link:RelNotes/2.12.0.txt[2.12].
+
* link:v2.11.1/git.html[documentation for release 2.11.1]
* release notes for
现在让我们使用你所谈论的形式,git reset [tree-ish] [--] [paths]
:
$ git reset HEAD^ -- Documentation/git.txt
Unstaged changes after reset:
M Documentation/git.txt
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: Documentation/git.txt
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: Documentation/git.txt
我们可以检查索引状态和HEAD
之间的差异如下:
$ git diff --staged
diff --git a/Documentation/git.txt b/Documentation/git.txt
index aa895da4a..4f208fab9 100644
--- a/Documentation/git.txt
+++ b/Documentation/git.txt
@@ -44,11 +44,6 @@ unreleased) version of Git, that is available from the 'master'
branch of the `git.git` repository.
Documentation for older releases are available here:
-* link:v2.12.0/git.html[documentation for release 2.12.0]
-
-* release notes for
- link:RelNotes/2.12.0.txt[2.12].
-
* link:v2.11.1/git.html[documentation for release 2.11.1]
* release notes for
请注意,这与HEAD
提交完全相反:我们索引中的Documentation/git.txt
版本与HEAD^
中记录的版本匹配。我们还可以将我们的工作树与HEAD
进行比较:
$ git diff HEAD
# no output, because there's no difference
我们还可以将工作树与索引进行比较:
$ git diff
diff --git a/Documentation/git.txt b/Documentation/git.txt
index 4f208fab9..aa895da4a 100644
--- a/Documentation/git.txt
+++ b/Documentation/git.txt
@@ -44,6 +44,11 @@ unreleased) version of Git, that is available from the 'master'
branch of the `git.git` repository.
Documentation for older releases are available here:
+* link:v2.12.0/git.html[documentation for release 2.12.0]
+
+* release notes for
+ link:RelNotes/2.12.0.txt[2.12].
+
* link:v2.11.1/git.html[documentation for release 2.11.1]
* release notes for
碰巧模仿HEAD
中的提交,因为我们的索引对应HEAD^
但我们的工作树仍然与HEAD
对齐! (这部分特别令人困惑,但是一旦你知道它是如何工作它会更有意义。)如果我们checkout
(因此将索引中的版本复制到工作树),这将不再是这种情况:
$ git checkout -- Documentation/git.txt
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: Documentation/git.txt
我们看到我们的工作树现在与索引匹配,实际上HEAD
和工作树之间的差异与HEAD
和索引之间的前一个差异对齐:
$ git diff HEAD
diff --git a/Documentation/git.txt b/Documentation/git.txt
index aa895da4a..4f208fab9 100644
--- a/Documentation/git.txt
+++ b/Documentation/git.txt
@@ -44,11 +44,6 @@ unreleased) version of Git, that is available from the 'master'
branch of the `git.git` repository.
Documentation for older releases are available here:
-* link:v2.12.0/git.html[documentation for release 2.12.0]
-
-* release notes for
- link:RelNotes/2.12.0.txt[2.12].
-
* link:v2.11.1/git.html[documentation for release 2.11.1]
* release notes for
这种组合(git reset HEAD^ -- Documentation/git.txt && git checkout -- Documentation/git.txt
)相当于git checkout HEAD^ -- Documentation/git.txt
。