我想拆分一个提交,但不确定使用哪个重置选项。
我正在看这页用简单的英语说,“git重置”是做什么的?,但我意识到我并不真正理解git索引或暂存区是什么,因此解释没有帮助。
此外,在我看来,在这个答案中——混合和——软的用例是相同的(当您想修复和重新承诺时)。有人能再详细解释一下吗?我知道,混合可能是最好的选择,但我想知道原因。最后,硬呢?
谁能给我一个工作流程的例子,如何选择3个选项会发生?
我想拆分一个提交,但不确定使用哪个重置选项。
我正在看这页用简单的英语说,“git重置”是做什么的?,但我意识到我并不真正理解git索引或暂存区是什么,因此解释没有帮助。
此外,在我看来,在这个答案中——混合和——软的用例是相同的(当您想修复和重新承诺时)。有人能再详细解释一下吗?我知道,混合可能是最好的选择,但我想知道原因。最后,硬呢?
谁能给我一个工作流程的例子,如何选择3个选项会发生?
当前回答
请注意,这是一个简化的解释,旨在作为寻求理解这个复杂功能的第一步。
对于那些想要在这些命令之后可视化他们的项目状态的视觉学习者来说,这可能是有帮助的:
给定:- A - B - C (master)
适用于开启彩色终端机的用户 (git配置——global color。ui汽车):
git重置-软A,你会看到B和C的东西在绿色(分期和准备提交)
git重置-混合A(或git重置A),你会看到B和C的东西在红色(未分期和准备分期(绿色),然后提交)
git重置-硬A,你将不再看到B和C的变化在任何地方(将好像他们从来没有存在过)
或者对于那些使用像“塔”或“SourceTree”这样的GUI程序的人
git重置——软A,你会看到B和C的东西在“阶段性文件”区域准备提交
git reset—混合A(或git reset A),你会看到B和C的东西在“unstaging files”区域准备移动到staging,然后提交
git重置-硬A,你将不再看到B和C的变化在任何地方(将好像他们从来没有存在过)
其他回答
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命令时,你会后悔什么,这样的描述可能更有帮助。假设我们有这个:
A - B - C - D <- HEAD
以下是一些可能会让你后悔的事情,以及应对方法:
1. 我很遗憾B, C, D不是一个人。
git重置——软A,我现在可以立即提交,从A开始的所有更改都是一次提交。
2. 我很遗憾B、C和D不是两次提交(或十次提交,或其他)。
提交已经消失,索引回到了A,但是工作区域看起来仍然和d之后一样。所以现在我可以在一个完全不同的分组中添加并提交。
3.我很遗憾B, C, D发生在这支树枝上;我希望我在A之后有一个分支它们发生在另一个分支上。
在另一个分支上创建一个新的分支,然后git重置——硬a。当前分支现在结束于a,其他分支起源于它,包含B、C和D。
(当然,你也可以使用硬重置,因为你希望B、C和D从未发生过。)
——soft:告诉Git将HEAD重置为另一个提交,这样索引和工作目录将不会以任何方式改变。在原始HEAD和提交之间更改的所有文件都将被暂存。
-mixed:就像软的,这将重置HEAD到另一个提交。它还将重置索引以匹配它,而工作目录将不会被触及。所有更改都将保留在工作目录中,并显示为已修改,但不是阶段性的。
——hard:这将重置所有内容——它将HEAD重置回另一个提交,重置索引以匹配它,并重置工作目录以匹配它。
-混合和-软之间的主要区别是你的索引是否也被修改。点击这里查看更多信息。
用最简单的话来说:
——soft:取消提交更改,更改是阶段性的(index)。 ——mixed(默认):uncommit + unstage变更,变更留在工作树中。 ——难:uncommit + unstage + delete changes,什么都不剩。
-混合vs -软vs -硬:
--mixed:
Delete changes from the local repository and staging area.
It won't touch the working directory.
Possible to revert back changes by using the following commands.
- git add
- git commit
Working tree won't be clean.
--soft:
Deleted changes only from the local repository.
It won't touch the staging area and working directory.
Possible to revert back changes by using the following command.
- git commit.
Working tree won't be clean
--hard:
Deleted changes from everywhere.
Not possible to revert changes.
The working tree will be clean.
注意:如果提交被确认到本地存储库并放弃这些提交,我们可以使用:
`git reset command`.
但是如果提交被确认到远程存储库,那么不建议使用reset命令,我们必须使用revert命令来丢弃远程提交。