简体   繁体   English

是否可以将解决方案修改为 git 中的合并冲突?

[英]Is it possible to revise the resolution to a merge conflict in git?

I am working on a tex file together with a collaborator using git (using gitkraken).我正在使用 git(使用 gitkraken)与合作者一起处理 tex 文件。 We work on the same branch, and at some point there were some merge conflicts when one of us pushed our latest work onto that branch.我们在同一个分支上工作,当我们中的一个人将我们的最新工作推送到该分支时,在某些时候出现了一些合并冲突。 Since then we have continued working on this file, but we have realized that we would like to "redo" resolving the merge conflicts from a couple of commits back.从那以后,我们一直在处理这个文件,但我们意识到我们想要“重做”解决来自几次提交的合并冲突。

Is there a clean way to do this?有没有干净的方法来做到这一点?

One thing I though of was to copy the version of the file in question (there is only one file of interest) and then just overwrite the current version of the file and then push that file onto the current branch, but I'm afraid this will overwrite some of the changes (that we'd like to keep) since the original conflict in question.我想到的一件事是复制有问题的文件版本(只有一个感兴趣的文件),然后覆盖文件的当前版本,然后将该文件推送到当前分支,但恐怕这将覆盖自有问题的原始冲突以来的一些更改(我们希望保留)。 Another way could be to copy the old version that has some of the changes we'd like to keep and then use a diff tool to merge in selectively the things we'd like to change.另一种方法是复制具有我们希望保留的某些更改的旧版本,然后使用差异工具有选择地合并我们希望更改的内容。

This last option is the best thing I could come up with.最后一个选项是我能想到的最好的方法。 Is there a better way?有没有更好的办法?

Every commit—including merge commits—simply holds a full and complete snapshot of all of your files.每次提交——包括合并提交——只是保存所有文件的完整快照。

Once you're collaborating with other people, it's usually not worth a lot of effort to go back and "fix" previous commits.一旦你与其他人合作,通常不值得花很多精力回去“修复”以前的提交。 In fact, that's technically impossible: you can't change any existing commits.事实上,这在技术上是不可能的:你不能改变任何现有的提交。 What you end up doing is providing new-and-improved alternative commits, in place of the originals.您最终要做的是提供新的和改进的替代提交,以代替原始提交。 For instance, suppose:例如,假设:

...--I--J
         \
          M--N--O   <-- branch (HEAD)
         /
...--K--L

is the current situation, ie, existing commits up through J and up through L got merged to make M , and you and they have since built N and O atop M .是当前的情况,即通过JL现有提交合并为M ,并且您和他们已经在M之上构建了NO You now discover that there's a "bug" in the snapshot stored in merge commit M due to some mis-resolution of conflicts.您现在发现由于某些冲突的错误解决,存储在合并提交M中的快照中存在“错误”。

The only way to make a corrected version of M is to make a new merge commit, M' .制作M的更正版本的唯一方法是进行新的合并提交, M' (There are several ways of doing that.) The result is: (有几种方法可以做到这一点。)结果是:

...--I--J__
        |  \
        |   M--N--O   <-- branch
         \ /
          x
         / \
...--K--L---M'  <-- newbranch (HEAD)

That is, bad merge M still exists even though you have now made new-and-improved merge M' on this new replacement branch newbranch .也就是说,即使您现在在这个新的替换分支newbranch上进行了新的和改进的合并M' ,错误合并M仍然存在 You now need to copy N and O as well, to new-and-improved N' and O' :您现在还需要将NO复制到新的和改进的N'O'

...--I--J__
        |  \
        |   M--N--O   <-- branch
         \ /
          x
         / \
...--K--L---M'-N'-O'  <-- newbranch (HEAD)

Now you need to have everyone—you and everyone else— stop using the old chain, MNO as found by name branch , and start using instead the new chain, M'-N'-O' .现在你需要让每个人——你和其他人——停止使用旧链, MNO如名称branch ,并开始使用链, M'-N'-O' In your Git, you can erase the name branch and rename newbranch to branch .您的Git 中,您可以删除名称branch并将newbranch重命名为branch In their Git, they'll have to do the same thing.他们的Git 中,他们必须做同样的事情。 Everyone must change over at the same time.每个人都必须同时改变。 This tends to be a lot of work.这往往需要大量工作。

It's usually better to just admit that the past has a bug in it.承认过去有错误通常会更好。 Make a new commit atop O that fixes the problem:O上进行新的提交以解决问题:

...--I--J
         \
          M--N--O--P   <-- branch (HEAD)
         /
...--K--L

Everyone can pick up new commit P as usual and just keep working as usual.每个人都可以像往常一样拿起新的提交P并继续像往常一样工作。 Sure, commits MNO have bad snapshots ... but so what?当然,提交MNO有糟糕的快照......但那又怎样? Bad stuff happens all the time;坏事总是发生; version control is not about preventing bad stuff, but rather about letting you correct it.版本控制不是为了防止坏东西,而是让你纠正它。 You've corrected it.你已经更正了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM