我想拆分一个提交,但不确定使用哪个重置选项。
我正在看这页用简单的英语说,“git重置”是做什么的?,但我意识到我并不真正理解git索引或暂存区是什么,因此解释没有帮助。
此外,在我看来,在这个答案中——混合和——软的用例是相同的(当您想修复和重新承诺时)。有人能再详细解释一下吗?我知道,混合可能是最好的选择,但我想知道原因。最后,硬呢?
谁能给我一个工作流程的例子,如何选择3个选项会发生?
我想拆分一个提交,但不确定使用哪个重置选项。
我正在看这页用简单的英语说,“git重置”是做什么的?,但我意识到我并不真正理解git索引或暂存区是什么,因此解释没有帮助。
此外,在我看来,在这个答案中——混合和——软的用例是相同的(当您想修复和重新承诺时)。有人能再详细解释一下吗?我知道,混合可能是最好的选择,但我想知道原因。最后,硬呢?
谁能给我一个工作流程的例子,如何选择3个选项会发生?
当前回答
在研究这三个选项之前,我们必须了解三件事。
1) 历史/头部
2)阶段/索引
3)工作目录
reset——soft:历史记录已更改,HEAD已更改,工作目录未更改。
reset——mixed:历史记录已更改,HEAD已更改,工作目录已更改。
reset——hard:历史记录更改,HEAD更改,工作目录更改,数据丢失。
使用Git总是安全的——软的。在复杂的需求中应该使用其他选项。
其他回答
在研究这三个选项之前,我们必须了解三件事。
1) 历史/头部
2)阶段/索引
3)工作目录
reset——soft:历史记录已更改,HEAD已更改,工作目录未更改。
reset——mixed:历史记录已更改,HEAD已更改,工作目录已更改。
reset——hard:历史记录更改,HEAD更改,工作目录更改,数据丢失。
使用Git总是安全的——软的。在复杂的需求中应该使用其他选项。
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 reset命令各选项的基本区别如下。
——soft:只重置HEAD到你选择的提交。工作原理与git签出基本相同,但不创建分离的头部状态。 ——mixed(默认选项):将HEAD重置为您在历史记录中选择的提交,并撤消索引中的更改。 ——hard:将HEAD重置为您在历史记录中选择的提交,撤消索引中的更改,并撤消工作目录中的更改。
mkarasek的回答很好,简单来说,我们可以说……
git reset——soft:将HEAD设置为预期的提交,但保持从上次提交开始的更改 Git重置-混合:它和Git重置一样-软,但唯一的区别是它从上次提交中UN stage你的更改 git reset——hard:在你指定的提交上设置HEAD,并重置你从上次提交的所有更改,包括未提交的更改。
——soft和——mixed有点相似,唯一的区别是,如果你想在暂存区中保留你的更改,使用——soft,如果你不想在暂存区中更改,使用——mixed。
当您在存储库中修改一个文件时,更改最初是暂不执行的。为了提交它,你必须使用git add来执行它——也就是说,将它添加到索引中。当你提交时,提交的更改是那些已经添加到索引中的更改。
git重置更改,至少,当前分支(HEAD)指向的位置。-混合和-软之间的区别在于索引是否也被修改。如果我们在master分支上进行这一系列的提交:
- A - B - C (master)
头指向C,索引匹配C。
当我们运行git reset -soft B, master(因此HEAD)现在指向B,但索引仍然有从C的变化;Git状态将显示它们为阶段性。如果我们在这里运行git commit,我们会得到一个新的commit,和C一样。
好的,再从这里开始:
- A - B - C (master)
现在让我们进行git重置-mixed b(注意:-mixed是默认选项)。同样,master和HEAD指向B,但这一次索引也被修改以匹配B。如果我们在这一点上运行git commit,什么也不会发生,因为索引匹配HEAD。我们在工作目录中仍然有更改,但由于它们不在索引中,git状态将它们显示为未暂存。要提交它们,你需要git添加,然后像往常一样提交。
And finally, --hard is the same as --mixed (it changes your HEAD and index), except that --hard also modifies your working directory. If we're at C and run git reset --hard B, then the changes added in C, as well as any uncommitted changes you have, will be removed, and the files in your working copy will match commit B. Since you can permanently lose changes this way, you should always run git status before doing a hard reset to make sure your working directory is clean or that you're okay with losing your uncommitted changes.
最后,一个可视化图: