Ano*_*noE 5 git merge conflict git-merge-conflict
Preface: this is not a general question about merge conflicts, but a very specific case that keeps bugging me. It is quite trivial, but it does amount to a (slight) hassle often enough to stand out. I am not concerned about general merging, this is just about saving a few seconds here and there for very mechanical conflict resolution, by avoiding said conflict in the first place. I am also absolutely aware that this is not a "git problem" or something like that.
That said, assume we have a source file of a class:
class Xyz
...
...
def last_method
...
end
end
Run Code Online (Sandbox Code Playgroud)
This starts out identical in master
and several feature branches. Now, as we implement our features, we add more methods to this class:
Branch 1:
class Xyz
...
...
def last_method
...
end
def new_method1
...
end
end
Run Code Online (Sandbox Code Playgroud)
Branch 2:
class Xyz
...
...
def last_method
...
end
def new_method2
...
end
end
Run Code Online (Sandbox Code Playgroud)
The new methods are not related and will happily coexist when both branches are merged back to master
.
Obviously this will lead to a merge conflict. The reason is clear - we changed the source file at exactly the same spot, and obviously git cannot (and should not) magically decide for us that this is not a "real" conflict; git would have to chose which of the methods should be placed first, etc.
One way to avoid the conflict would be to insert the new methods at different places in the file (assuming the order does not matter), but we really don't want to spend much effort (or any at all, actually) to mentally keep track where to insert stuff or what happens in other features.
The question, then: is there another way, maybe some coding convention, that you are regularly applying, which somehow avoids this merge conflict?
这是一个很好的问题。但是,在某些条件下,有一些方法可以缓解该问题。
在理想情况下,在设计类时,您决定它将由什么组成(变量、方法等),并且您已经可以为它们选择适当的名称。在这种情况下,您应该在引入该类的提交中编写这些方法的存根。
然后,这些存根将充当基于行的版本控制系统(例如 Git)的“锚”:
class MyClass
def initialize
# TODO
end
def get_ID
# TODO
end
def set_ID
# TODO
end
end
Run Code Online (Sandbox Code Playgroud)
在这个“第一次”提交之后,不同的贡献者可以自由地更改不同方法的主体:在我的示例中,Alice 可以实现get_ID
,Bob 可以实现,而set_ID
不必担心将来会遇到合并冲突,因为每个方法的def
和行end
已经存在于原始提交中。
归档时间: |
|
查看次数: |
117 次 |
最近记录: |