git 重置命令的文档不明确<path>



摘自文档:git reset <path>

运行 git reset 更新索引条目后,您可以使用 git-checkout[1] 将索引中的内容签出到工作树中。或者,使用 git-checkout[1] 并指定提交,您可以一次性将路径的内容从提交复制到索引和工作树。

:有人可以举例说明所引用的paragrah的含义吗?


注意此问题专门询问git reset [-q] [<tree-ish>] [--] <paths>…​变体的引用段落。我知道git reset <path>做了什么,但无法理解上面的片段

  • 引用的paragrah的第一部分的例子

    运行 git reset 更新索引条目后,您可以使用 git-checkout[1] 将索引中的内容签出到工作树中。

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.

  • 引用的paragrah第二部分的示例

    或者,使用 git-checkout[1] 并指定提交,您可以一次性将路径的内容从提交复制到索引和工作树。

开始时恢复到原始状态:

$ 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 <gitster@pobox.com>
Date:   Fri Feb 24 10:49:58 2017 -0800
Git 2.12
Signed-off-by: Junio C Hamano <gitster@pobox.com>
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 <gitster@pobox.com>
Date:   Fri Feb 24 10:49:58 2017 -0800
Git 2.12
Signed-off-by: Junio C Hamano <gitster@pobox.com>
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

相关内容

最新更新