我想拆分一个提交,但不确定使用哪个重置选项。

我正在看这页用简单的英语说,“git重置”是做什么的?,但我意识到我并不真正理解git索引或暂存区是什么,因此解释没有帮助。

此外,在我看来,在这个答案中——混合和——软的用例是相同的(当您想修复和重新承诺时)。有人能再详细解释一下吗?我知道,混合可能是最好的选择,但我想知道原因。最后,硬呢?

谁能给我一个工作流程的例子,如何选择3个选项会发生?


当前回答

I’m not a git expert and just arrived on this forum to understand it! Thus maybe my explanation is not perfect, sorry for that. I found all the other answer helpful and I will just try to give another perspective. I will modify a bit the question since I guess that it was maybe the intent of the author: “I’m new to git. Before using git, I was renaming my files like this: main.c, main_1.c, main_2.c when i was performing majors changes in order to be able to go back in case of trouble. Thus, if I decided to come back to main_1.c, it was easy and I also keep main_2.c and main_3.c since I could also need them later. How can I easily do the same thing using git?” For my answer, I mainly use the “regret number three” of the great answer of Matt above because I also think that the initial question is about “what do I do if I have regret when using git?”. At the beginning, the situation is like that:

a b c d(主)

第一个要点是创建一个新分支:git分支mynewbranch。然后一个得到:

(master和mynewbranch)

让我们假设现在有人想要回到A(前3次提交)。第二个要点是使用git reset命令——即使人们可以在网上读到这是危险的,也很难。是的,这很危险,但仅适用于未提交的更改。因此,这样做的方法是:

Git重置——commita的硬数字

or

Git复位-硬主~3

那么就得到: A (master) - B - C - D (mynewbranch)

Then, it’s possible to continue working and commit from A (master) but still can get an easy access to the other versions by checking out on the other branch: git checkout mynewbranch. Now, let’s imagine that one forgot to create a new branch before the command git reset --hard. Is the commit B, C, D are lost? No, but there are not stored in any branches. To find them again, one may use the command : git reflog that is consider as “a safety command”( “in case of trouble, keep calm and use git reflog”). This command will list all commits even those that not belong to any branches. Thus, it’s a convenient way to find the commit B, C or D.

其他回答

——soft:告诉Git将HEAD重置为另一个提交,这样索引和工作目录将不会以任何方式改变。在原始HEAD和提交之间更改的所有文件都将被暂存。

-mixed:就像软的,这将重置HEAD到另一个提交。它还将重置索引以匹配它,而工作目录将不会被触及。所有更改都将保留在工作目录中,并显示为已修改,但不是阶段性的。

——hard:这将重置所有内容——它将HEAD重置回另一个提交,重置索引以匹配它,并重置工作目录以匹配它。

-混合和-软之间的主要区别是你的索引是否也被修改。点击这里查看更多信息。

简单回答这3个选项在什么情况下使用:

在代码中保留当前的更改,但要重写提交历史:

soft:你可以一次性提交所有内容,并使用新的描述创建一个新的提交(如果你使用tootise git或任何其他大多数gui,这是一个可以使用的,因为你仍然可以在提交中选择你想要的文件,并以这种方式对不同的文件进行多次提交。在Sourcetree中,所有文件都将被提交。) mixed:在提交之前,您必须将单独的文件再次添加到索引中(在Sourcetree中,所有更改的文件都将被取消staging)

要真正丢失您在代码中的更改:

难的是:你不仅重写了历史,而且还失去了你重置之前的所有更改

mkarasek的回答很好,简单来说,我们可以说……

git reset——soft:将HEAD设置为预期的提交,但保持从上次提交开始的更改 Git重置-混合:它和Git重置一样-软,但唯一的区别是它从上次提交中UN stage你的更改 git reset——hard:在你指定的提交上设置HEAD,并重置你从上次提交的所有更改,包括未提交的更改。

——soft和——mixed有点相似,唯一的区别是,如果你想在暂存区中保留你的更改,使用——soft,如果你不想在暂存区中更改,使用——mixed。

在这些情况下,我喜欢能够解释这一点的视觉效果:

Git重置-[硬/混合/软]:

所以每一个都影响不同的范围:

Hard =>工作目录+索引+ HEAD 混合=>指数+头部 软=>头只(索引和工作目录不变)。

所有类型的重置都会改变回购中的HEAD。此外…… >将从repo中删除的提交的更改移动到索引中,并合并已经存在的任何更改。 Git reset——hard <b>丢失工作树和索引中的更改。 它是唯一更改工作树的方法。