不清楚git reset <path>命令的文档

时间:2017-02-24 19:39:31

标签: git git-reset

取自docs: git reset <path>

  

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

有人能举例说明引用的paragrah的含义吗?

注意此问题明确要求git reset [-q] [<tree-ish>] [--] <paths>…​变体的引用段落。我知道git reset <path>做了什么,但对上面的代码片段没有任何意义

2 个答案:

答案 0 :(得分:1)

  • 引用段落的第一部分的示例
      

    运行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

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

    或者,使用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的两种用途:

git checkout -- hello.c

&安培;

git checkout HEAD hello.c

没有命名提交的第一个是从索引(到工作树)复制内容,而后者是从提交历史(到索引和工作树)。

答案 1 :(得分:0)

我建议阅读Pro Git的前三章,这将使您更好地理解Git模型,这对理解这一点非常关键。

tl; dr是这个版本的reset只更新索引(即缓存,暂存区域)而不触及工作树(您可以看到的文件)或HEAD,如果这样做,您可以使用checkout使您的工作树与索引保持一致(或者只使用checkout执行这两项操作 - 获取特定版本的文件并使用{s}更新您的索引和工作树他们 - 一气呵成。)

我们以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