您能否在不造成冲突的情况下还原除上一次提交以外的其他提交? [英] Can you ever revert a commit other than the very last without creating a conflict?

查看:69
本文介绍了您能否在不造成冲突的情况下还原除上一次提交以外的其他提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:stackoverflow的bot认为该问题是主观的,但这是事实-我在这里不是要征求意见!

Note: stackoverflow's bot thinks the question is subjective but it is very factual - I am not asking for opinions here!

假设我只有一个分支,而我的提交历史如下: A-B-C-D

Let's say that I have one branch only, and the history of my commits are: A - B - C - D

如果我在GUI(例如GitKraken或Windows的Git)中单击C并执行还原提交",则会收到文件冲突消息.

If I click on C in a GUI (like GitKraken or Git for Windows) and do "revert commit", I get a file conflict message.

两个问题:

  1. 这是因为还原撤消"了C和GIT中所做的更改 现在卡在B和D上,这会以不同的方式修改文件, 不兼容的方式?是这个原因吗?

  1. Is this because the revert "undoes" the changes made in C, and GIT is now stuck with B and D which modify the file in different, incompatible ways? is that the reason?

如果是,这是否意味着您只能还原最后一个 犯罪?除了最后一次提交之外,您还能恢复一次提交吗? 不会造成冲突?

If yes, does this mean that you can only revert the very last commit? Can you ever revert a commit other than the very last without creating a conflict?

更新:在@RomainValeri做出非常有帮助的说明后,编辑问题.我认为我在还原和重置之间造成了太多混乱.在下面的示例中,

UPDATE: editing the question after the very helpful clarifications from @RomainValeri. I think I was making too much confusion between revert and reset. In his example below, doing

git revert B

导致分支来自

A-B-C-D

A-B-C-D-E

如果B是唯一更改了file2.txt的提交,并且B没有对其他文件进行任何其他更改,则还原后创建的新提交E将保留在C和D中所做的所有更改但不是B制造的.这是正确的吗?

这是因为从技术上讲,还原意味着取消,撤消提交的任何更改-这是正确的吗?

This is because, technically, reverting means cancelling out, undoing any changes of a commit - is this correct?

也:假设我的工作目录中只有一个文件.如果B是唯一更改文件中fun1()函数的提交,,而所有其他提交都更改了同一文件中的其他函数,则还原B最有可能导致冲突 ,因为git根据文件中的行而不是文件中的函数进行思考.这是正确的吗?

Also: let's say that I have only one file in my working directory. If B is the only commit which changes the function fun1() in my file, while all the other commits change other functions within the same file, then reverting B will most likely cause a conflict, because git thinks in terms of lines in a file, not in terms of functions within a file. Is this correct?

因此,假设B更改了fun1(),C更改了fun2(),D更改了fun3().然后,我意识到在fun1()的B处所做的更改是错误的,我需要撤消它们. 如果所有这些功能都在同一个文件中,是否可以撤消B中的更改,同时保留C和D中的更改?

So let's say that B changes fun1(), C changes fun2() and D changes fun3(). I then realise that the changes made at B in fun1() are wrong and I need to undo them. If all these functions are in the same file, is there a way to undo the changes in B while retaining those in C and D?

相反,如果这3个函数中的每一个都在单独的文件中,那么撤消一个提交的更改而不影响其他提交会更简单,对吗?

If, instead, each of these 3 functions is in a separate file, then it is much simpler to undo the changes of one commit without affecting the others, right?

我想当然是单个文件不应太大或包含太多执行不同功能的函数的众多原因之一,对吧?

I imagine this is, of course, one of the many reasons why a single file should never be too large nor contain too many functions doing different things, right?

推荐答案

简短答案:是.您可以很好还原给定的提交(甚至是旧的提交)并且没有冲突.

Short answer : Yes. You could very well revert a given commit (even an old one) and have no conflict.

为什么会这样?

git revert 在当前提示的顶部创建一个新提交树,它不会删除给定的提交,也不会以任何方式对其进行修改.

git revert creates a new commit on top of the current tip of the tree, it does not delete the given commit, nor modifies it in any way.

合并基础中的内容有所不同时,您会遇到冲突在您使用还原创建的提交和HEAD之间.

You have a conflict when something is different in the merge-base between the commit you're creating with the revert and HEAD.

为此自己尝试一下

竞争示例

给出回购树,

Folder1
  file1.txt
  file2.txt
Folder2
  file3.txt

及其后的历史

A---B---C---D << HEAD

其中

A创建具有其内容的所有文件.
B修改file2.txt
C修改file1.txtfile3.txt
D进一步修改file1.txt

A creates all files with their contents.
B modifies file2.txt
C modifies file1.txt and file3.txt
D further modifies file1.txt

然后输入命令

git revert B

不会不会产生任何冲突,只是重新进行新的提交E,并对file2.txt进行新的更改.

would not create any conflict, just a fresh new commit E with new changes for file2.txt.

A---B---C---D---E << HEAD


git revert 创建的新提交的内容(在注释后添加)

E中的更改是B引入的完全相反的更改.

The changes in E are the exact reverse changes that B introduced.

使用B这样:

$ git show B
commit f3ba29d98c0998297126d686d03d33794cbc0f73
Author: Pythonista <some.name@some.isp>
Date:   Sun Mar 24 20:41:49 2019 +0100

    Some change

diff --git a/javascript/functions.js b/javascript/functions.js
index 543c9cb..5f166d3 100644
--- a/javascript/functions.js
+++ b/javascript/functions.js
@@ -3710,5 +3710,6 @@ function dumpLists(isMixted) {
 // end of process

 createTrigger(window, 'load', START_EVERYTHING);
+doStuff(param);

您将有一个E提交,其中包含:

You would have an E commit containing :

$ git show E
commit 2ff723970e81d64f3776c081a1f96242589774b6 (HEAD -> master)
Author: Pythonista <some.name@some.isp>
Date:   Sun Mar 24 20:42:33 2019 +0100

    Revert "Some change"

    This reverts commit f3ba29d98c0998297126d686d03d33794cbc0f73.

diff --git a/javascript/functions.js b/javascript/functions.js
index 5f166d3..543c9cb 100644
--- a/javascript/functions.js
+++ b/javascript/functions.js
@@ -3710,6 +3710,5 @@ function dumpLists(isMixted) {
 // end of process

 createTrigger(window, 'load', START_EVERYTHING);
-doStuff(param);

在最后一个差异行的前面加上+-,表示加法"(+)和删除"(-).

Mind the + or - in front of the last diff line, meaning "addition" (+) and "deletion" (-).

最后,在您的帖子结尾说一句关于关注的话题(出于某种原因,我最想念的是):

Finally, to say a word about the concerns at the end of your post (which I mostly missed, for some reason) :

我不认为您应该基于对源代码控制的担忧而为应用程序选择体系结构. Git是一个很好的工具,它足够聪明,可以防止我们不可避免地会遇到的频繁麻烦中的很大一部分.寻求最佳方法来按照自己的逻辑对代码进行分组,而与git无关.

I don't think you should base architecture choices for your application upon source control worries. Git is quite a great tool, and it's smart enough to prevent a big part of the frequent trouble we would inevitably have without. Go for the best way to group your code along its own logic, regardless of git.

顺便说一句,冲突检测发生在块级别,而不是文件级别.意味着如果同一文件出现在不同的非连续部分中,则可以由两个源修改同一文件而不会引起冲突.我无法确切地说阈值在哪里,但这与重点无关.

And by the way, conflict detection happens at chunk level, not file. Meaning that the same file could have been modified by two sources without provoking a conflict if they happen in different non-contiguous parts. I couldn't exactly say where's the threshold, but it's irrelevant to the point.

这篇关于您能否在不造成冲突的情况下还原除上一次提交以外的其他提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆