我想拆分一个提交,但不确定使用哪个重置选项。
我正在看这页用简单的英语说,“git重置”是做什么的?,但我意识到我并不真正理解git索引或暂存区是什么,因此解释没有帮助。
此外,在我看来,在这个答案中——混合和——软的用例是相同的(当您想修复和重新承诺时)。有人能再详细解释一下吗?我知道,混合可能是最好的选择,但我想知道原因。最后,硬呢?
谁能给我一个工作流程的例子,如何选择3个选项会发生?
我想拆分一个提交,但不确定使用哪个重置选项。
我正在看这页用简单的英语说,“git重置”是做什么的?,但我意识到我并不真正理解git索引或暂存区是什么,因此解释没有帮助。
此外,在我看来,在这个答案中——混合和——软的用例是相同的(当您想修复和重新承诺时)。有人能再详细解释一下吗?我知道,混合可能是最好的选择,但我想知道原因。最后,硬呢?
谁能给我一个工作流程的例子,如何选择3个选项会发生?
当前回答
mkarasek的回答很好,简单来说,我们可以说……
git reset——soft:将HEAD设置为预期的提交,但保持从上次提交开始的更改 Git重置-混合:它和Git重置一样-软,但唯一的区别是它从上次提交中UN stage你的更改 git reset——hard:在你指定的提交上设置HEAD,并重置你从上次提交的所有更改,包括未提交的更改。
——soft和——mixed有点相似,唯一的区别是,如果你想在暂存区中保留你的更改,使用——soft,如果你不想在暂存区中更改,使用——mixed。
其他回答
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.
在这些情况下,我喜欢能够解释这一点的视觉效果:
Git重置-[硬/混合/软]:
所以每一个都影响不同的范围:
Hard =>工作目录+索引+ HEAD 混合=>指数+头部 软=>头只(索引和工作目录不变)。
这里有许多关于git重置的误解的答案——软。虽然在特定的条件下git会重置——soft只会改变HEAD(从分离的HEAD状态开始),通常(对于预期的用途),它会移动您当前签出的分支引用。当然,如果你没有签出分支,它就不能这样做(因此git重置的特定条件-soft只会改变HEAD)。
我发现这是考虑git重置的最佳方式。你不只是移动HEAD(所有事情都是这样),你也移动了分支引用,例如master。这类似于运行git commit(当前分支与HEAD一起移动)所发生的情况,只是您没有创建(并移动到)一个新的提交,而是移动到先前的提交。
这是重置的重点,将分支更改为新提交以外的内容,而不是更改HEAD。你可以在文档示例中看到:
撤销提交,使其成为主题分支 $ git分支主题/wip (1) $ git重置-硬头~3 (2) $ git结帐主题/wip (3) 您已经提交了一些文件,但是意识到它们还不适合放在“主”分支中。你想在topic分支中继续打磨它们,所以在当前HEAD的基础上创建“topic/wip”分支。 倒回主分支以摆脱这三个提交。 切换到“topic/wip”分支并继续工作。
这一系列命令的意义是什么?你想移动一个分支,master,当你签出master时,你运行git reset。
这里投票最多的答案一般都很好,但我想我应该加上这个来纠正几个有误解的答案。
改变你的分支
Git reset——soft <ref>:重置当前检出分支的分支指针到指定引用处的提交,<ref>。工作目录和索引中的文件不会被更改。从这个阶段提交将把你带回到git reset命令之前的状态。
改变你的索引
Git重置——mixed <ref>
或者同样的
Git重置<ref>:
做什么——soft做AND还重置索引以匹配指定引用的提交。虽然git reset——soft HEAD什么都不做(因为它说将检出的分支移动到检出的分支),但git reset——mixed HEAD,或等价于git reset HEAD,是一个常见且有用的命令,因为它将索引重置为上次提交的状态。
也要更改工作目录
git reset——hard <ref>: does what——mixed does AND也覆盖你的工作目录。这个命令类似于git checkout <ref>,除了(这是关于重置的关键点)所有形式的git重置移动ref HEAD指向的分支。
关于“某某命令移动HEAD”的注释:
说命令移动HEAD是没有用的。任何改变您在提交历史中的位置的命令都会移动HEAD。这就是HEAD,一个指向你所在位置的指针。head就是你,所以无论你做什么,它都会移动。
莫·阿里用最简单的方式解释了这一点,下面是另一个简单的解释:
——soft:重置HEAD指针指向上一次提交
——mixed:——soft + delete添加的更改
——hard:——混合+恢复工作树文件的变化(小心!)
用最简单的话来说:
——soft:取消提交更改,更改是阶段性的(index)。 ——mixed(默认):uncommit + unstage变更,变更留在工作树中。 ——难:uncommit + unstage + delete changes,什么都不剩。