恢复较早的提交的一部分
我知道我缺少一些明显的东西,但是...假设我有3个承诺会影响相同的两个文件:a和b。我想恢复第二次提交中A中所做的更改,但我不想失去第三个提交中b的更改。如果我还原然后推荐,这不会在第三次提交中覆盖我对B的新更改?
I know I'm missing something obvious but... let's say I have 3 commits affecting the same two files: a and b. I want to revert the changes made in the 2nd commit to a, but I don't want to lose the changes made in the third commit to b. If I revert and then recommit, won't that overwrite my new changes for b in the third commit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
tl; dr:听起来您打算做 partial commit 2的偏移,如果您这样做,则 no ,file <代码> B 将根本不更改。
详细信息:基于问题和评论,我们知道:
a
和b
。恢复
commit 2,但基于评论,该提交中只有文件 a :那么重要的是要意识到
git还原
只是一种便利和约定,您可以准确地执行恢复如果您愿意,可以手动。无论您是使用
还原
命令还是手动执行,最终结果将是一个新提交(我们称其为“提交4”),它揭示了以前的提交的结果。在您的情况下,您要撤消Commit 2的A 部分,因此您可以手动撤消文件a
的更改,然后手动创建commit 4。我假设,如果您手动进行操作,则不会指望文件b
会受到任何影响。您也可以使用
还原
命令来帮助您制作提交4,例如:如果您使用
还原
命令,则GIT提出了有用的提交消息。但是,如果您以某种方式修改恢复,我建议修改这样的提交消息:附加说明:如果执行常规还原而不是上述部分还原,则更改文件<代码> b COMM 2中的代码也将被撤消。请参阅 bk2204的答案有关此方法的工作方式的更多详细信息。
tl;dr: It sounds like you intend to do a partial revert of commit 2, and if you do that, then No, file
b
will not be changed at all.Details: Based on the question and the comments, we know:
a
andb
.revert
commit 2, but only filea
in that commit, based on the comment:It's important to realize that
git revert
is merely a convenience and a convention, and you could do exactly whatrevert
does manually, if you wanted to. Regardless of whether you use therevert
command or do it manually, the end result will be a new commit, (let's call it commit 4), that undoes the result of a previous commit. In your case, you want to undo a portion of commit 2, so you could manually undo the change of filea
and create commit 4 manually. I assume if you did it manually you would not expect fileb
to be affected in any way.You could also use the
revert
command to assist you with making commit 4, for example:If you use the
revert
command, Git proposes a useful commit message. However, if you modify the revert in some way, I recommend modifying the commit message like this:Additional Note: if you do a regular revert instead of the partial revert described above, then the changes to file
b
in commit 2 would also be undone. See bk2204's answer for more details about how this works.当Git执行恢复时,它实际上将其用于该提交并将其倒转,然后将其应用于工作树和索引。它使用与樱桃挑选的机械以及引擎盖下的某些形式的重击,并且工作非常相似。
如果您在恢复中所做的更改位于文件的不同部分与您在Commit 3中所做的更改相比,将恢复清洁,您将进行提交1和3的更改,而不是2。如果恢复的变化与提交3的更改重叠(或几乎重叠),然后您可能会发生冲突,您必须解决继续下去。在任何情况下,git都不会简单地撤消提交3中的更改3,因为您要求恢复commit2。
这就是为什么当您按顺序恢复多个提交时,您想在早期的较早之前恢复较晚的提交,因为这会消除他们最初完成的顺序变化,最大程度地减少了冲突的风险。
When Git performs a revert, it essentially takes the patch for that commit and reverses it, then applies it to the working tree and index. It uses the same machinery as cherry-pick and some forms of rebase under the hood to do so and works very similarly.
If the changes you're making in the revert are in different parts of the file from the changes you made in commit 3, then the revert will apply cleanly, and you will have the changes from commits 1 and 3 but not 2. If the changes in your revert overlap (or nearly overlap) with the changes in commit 3, then you'll probably get a conflict, which you'll have to resolve to continue. In no case is Git going to simply undo the changes in commit 3 because you asked to revert commit 2.
This is why when you are reverting multiple commits in sequence, you want to revert the later ones before the earlier ones, because that undoes the changes in the order they were originally done, minimizing the risk of conflicts.