英文:
Git merge, but adding in all the conflicting lines
问题
code.go
在main
分支上当前包含以下内容:
package code
some Golang code() {
}
code.go
在new-branch
分支上当前包含以下内容:
package code
some other Golang code() {
}
even more code() {
}
理想情况下,我希望最终结果是:
package code
some Golang code() {
}
some other Golang code() {
}
even more code() {
}
我该如何将new-branch
合并到main
分支以实现上述结果?当我合并时,git
给出了冲突,因为some Golang code()
和some other Golang code()
在分支之间的同一行上。
如果我选择"Accept both incoming",一个大括号会丢失,导致语法错误。我只想要最终的code.go
文件包含这三个代码块的完整形式。
英文:
code.go
on branch main
currently contains:
package code
some Golang code() {
}
code.go
on branch new-branch
currently has:
package code
some other Golang code() {
}
even more code() {
}
Ideally, I want the end result to be:
package code
some Golang code() {
}
some other Golang code() {
}
even more code() {
}
How can I "merge" the new-branch
onto main
to achieve the above result? When I merge, git
gave me conflict, since some Golang code()
and some other Golangcode()
are on the same line between branches.
If I choose "Accept both incoming," one of my brace goes missing, resulting in syntax error. I simply want the resulting code.go
file to contain all those 3 block of codes in their intact form.
答案1
得分: 2
问题在于git无法确定哪段代码应该先执行:将some Golang code
移到最后与将其放在开头一样自然。因此,手动合并是解决的方法,这样你可以确保代码按正确的顺序执行。
英文:
The issue here is that there is no way for git to determine which code should go first: moving the some Golang code
to the end instead would be just as natural as having it at the beginning. So doing the merge by hand is the way to go, so that you can make sure the code ends up in the correct order.
答案2
得分: 1
你不能自动完成它,因为你在这里有一个“经典”冲突(我在GUI中展示它,但Git内部看到的是相同的情况):一行代码被另一段代码替换。
你必须手动执行合并操作,并使用任何(好的)由Git支持的合并工具手动解决冲突。
英文:
You will not be do it automatically, because you have here "classic" conflict (I show it here in GUI, but internally Git see the same thing): one line of code replaced by another snippet
You must to perform manual merge and resolve conflict by hand using any (good) merge-tool, supported by Git
答案3
得分: 0
如果我选择“接受两者”,其中一个大括号会消失,导致语法错误。我只想要生成的code.go文件以其完整形式包含这3个代码块。
不幸的是,git和其他常见的版本控制系统使用的合并算法并不“代码感知”,它们只在文本级别上操作。
因此,虽然对于您来说很明显哪些行应该放在一起,但自动化工具必须尝试猜测哪些行应该放在哪里,而不知道它们的含义。从工具的角度来看,保持文件的结构,但用无意义的词语和标点符号替换它们:
habble hibble
quig quag quog blib-blob frong
gnorf
和
habble hibble
quig quaggledy quog blib-blob frong
gnorf
quoogle quog blib-blob frong
gnorf
合并算法看到两个分支在第3、6和7行添加了不同的文本,但在第4行都有“gnorf”。因此,它假设那是相同的“gnorf”,并给出类似于以下的结果:
habble hibble
quig quag quog blib-blob frong
quig quaggledy quog blib-blob frong
gnorf
quoogle quog blib-blob frong
gnorf
该算法可以更“笨一点”,不寻找您在两个分支中都添加了相同代码的情况,但这样做同样经常是错误的。
英文:
> If I choose "Accept both incoming," one of my brace goes missing, resulting in syntax error. I simply want the resulting code.go file to contain all those 3 block of codes in their intact form.
Unfortunately, the merge algorithms used by git and other common VCSes are not "code aware", they are operating only on the level of text.
So, while it is obvious to you which lines belong together, an automated tool has to try and guess which lines should go where with no idea what they mean. To see things from the tool's point of view, keep the structure of the files but replace the words and punctuation with nonsense:
habble hibble
quig quag quog blib-blob frong
gnorf
and
habble hibble
quig quaggledy quog blib-blob frong
gnorf
quoogle quog blib-blob frong
gnorf
The merge algorithm sees that the two branches have added different text at lines 3, 6, and 7, but both have "gnorf" at line 4. So it assumes that's the same "gnorf", and gives you something like this:
habble hibble
quig quag quog blib-blob frong
quig quaggledy quog blib-blob frong
gnorf
quoogle quog blib-blob frong
gnorf
The algorithm could be less clever, and not look for cases where you've added the same code in both branches, but this would be wrong just as often.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论