git reset --soft HEAD〜是否修改分段快照? [英] git reset --soft HEAD~ modifies staged snapshot?

查看:47
本文介绍了git reset --soft HEAD〜是否修改分段快照?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我所读过的有关 git reset --soft 的内容中,我了解到它并没有修改登台区域或索引.但是在下面的示例中,您可以清楚地看到由于 git reset --soft 而修改了临时区域/索引.为什么?

  mkdir test; cd test; git init;触摸回声1>> a; git添加a; git commit -m"1" a;回声2>> a; git commit -m"2" a;git diff-缓存;<没有输出>git reset --soft HEAD〜;git diff --cached;<参见下面的输出>-一个+++ b/a@@ -1 +1,2 @@1个+2 

解决方案

关于 git reset --soft 保留索引/临时区域未修改的说法是正确的.

请注意, git diff 总是比较两件事.它比较哪两件事?对于 git diff 的完全通用形式,答案有些复杂,但是您使用的是 git diff --cached ,它比较了 git diff 文档`:

git diff [-options ] --cached [ < commit> ] [-] [ < path>... ]

此表单用于查看您为与命名的 < commit> 相关的下一次提交所做的更改.通常,您需要与最新提交进行比较,因此,如果您不提供 < commit> ,则默认为 HEAD .[snip]

因此,要比较的两件事是 HEAD 和索引.

在旁边:文档具有误导性

以上内容暗示索引存储了更改,这是不正确的!索引/暂存区只是如果您立即进行下一次提交,则在下一次提交中将是.因此,在进行提交后,索引和当前提交是相同的-至少它们表示相同的工作树(索引省略了提交的重要部分,即作者和提交者名称+电子邮件+日期,父指针和提交消息-或简而言之,索引只有 个工作树,而且它的格式不寻常,更适合用作索引/缓存而不是提交)./p>

返回到 git reset git diff --cached

git reset --soft 修改 HEAD .它不会修改索引.您首先将 HEAD 与索引进行比较,然后看不到任何输出.然后,您更改 HEAD .现在,您将更改后的 HEAD 与索引进行比较,然后查看输出.

哪些更改导致输出更改?好吧, HEAD 更改了.

通过省略 git reset --soft 步骤并提供特定的提交,您可以看到相同的内容:

  git diff-缓存的HEAD〜 

这会将提交 HEAD〜与索引进行比较,并向您显示相同的输出.

From whatever I have read about git reset --soft, I learned that it doesn't modified staging area or the index. But in the below example you can see clearly that the staging area/index is modified as a result of git reset --soft. Why?

mkdir test;cd test;git init;
touch a;
echo 1 >>a;git add a;git commit -m"1" a;
echo 2 >>a;git commit -m"2" a;

git diff --cached; <NO OUTPUT>
git reset --soft HEAD~;
git diff --cached;<See the below output>

--- a/a
+++ b/a
@@ -1 +1,2 @@
 1
+2

解决方案

The claim that git reset --soft leaves the index/staging-area unmodified is correct.

Note that git diff always compares two things. Which two things does it compare? The answer is a bit complicated for the fully general form of git diff, but you are using git diff --cached, and it compares the two things noted in the git diff documentation`:

git diff [--options] --cached [<commit>] [--] [<path> ...]

This form is to view the changes you staged for the next commit relative to the named <commit>. Typically you would want comparison with the latest commit, so if you do not give <commit>, it defaults to HEAD. [snip]

Thus, the two things being compared are HEAD and the index.

Aside: the documentation is misleading

The above implies that the index stores changes, which is not true! The index / staging area is simply what would be in the next commit if you made that commit right now. Hence, after making a commit, the index and the current commit are identical—at least, they represent the same work-tree (the index omits significant parts of a commit, namely author and committer name+email+date, parent pointers, and commit message—or to put it a little more briefly, the index has only a work tree, and it's in an unusual form, more suited for being an index/cache than for being a commit).

Back to git reset and git diff --cached

git reset --soft modifies HEAD. It does not modify the index. You first compare HEAD against the index and see no output. Then you change HEAD. Now you compare the changed HEAD against the index, and see output.

What changed to cause the changed output? Well, HEAD changed.

You can see the same thing by omitting the git reset --soft step and providing a specific commit:

git diff --cached HEAD~

This will compare commit HEAD~ against the index, and show you the same output.

这篇关于git reset --soft HEAD〜是否修改分段快照?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆