简体   繁体   English

如何解决 monorepo 中的合并冲突?

[英]How to resolve merge conflicts in a monorepo?

Assume the following situation:假设以下情况:

  • the monorepo contains two artifacts in separate folders, say frontend and backend monorepo 在不同的文件夹中包含两个工件,比如前端和后端
  • frontend developers are not capable of solving merge conflicts in backend code前端开发人员无法解决后端代码中的合并冲突
  • backend developers are not capable of solving merge conflicts in frontend code后端开发人员无法解决前端代码中的合并冲突
  • feature-branch based workflow where frontend and backend developers work in parallel on the same feature branch基于功能分支的工作流,其中前端和后端开发人员在同一功能分支上并行工作

The problem arises when changes (created by separate developers) on a feature branch conflict in both, backend and frontend code.当后端和前端代码中的功能分支上的更改(由单独的开发人员创建)发生冲突时,就会出现问题。 A single developer (given the mentioned assumptions) is not able to conduct an update-merge on his own.单个开发人员(鉴于上述假设)无法自行进行更新合并。 问题说明

What is the best practice to resolve merge conflicts which span frontend and backend code?解决跨越前端后端代码的合并冲突的最佳实践是什么? If the vertical feature-branch based workflow is the underlying problem, how would you improve this setup while sticking with the first three assumptions?如果基于垂直特征分支的工作流是潜在问题,您将如何在坚持前三个假设的同时改进此设置?

Here is a blunt, technical way to split the merge conflict resolution in two distinct parts :这是将合并冲突解决方案分为两个不同部分的一种直率的技术方法:

  1. from the master branch, create two phony commits, one which carries only the changes on backend , one which carries only the changes on frontendmaster分支,创建两个虚假提交,一个只承载backend的更改,一个只承载frontend的更改

  2. have two developpers merge each branch into feature让两个开发人员将每个分支合并到feature

  3. merge feature into masterfeature合并到master


  1. split the changes in two :将更改分为两部分:
# starting situation :
*--X--*--*--Z <- master      # X is the fork point
    \                        # Z is the tip of branch master
     \
      *--*--*--T <- feature   # T is the tip of branch feature

Run the following commands :运行以下命令:

# from master, create a phony branch for backend :
$ git checkout -b backend master
$ git checkout X -- frontend/   # reset the content of 'frontend/' to its state in commit X 
$ git commit

# similarly for fontend :
$ git checkout -b frontend master
$ git checkout X -- backend/
$ git commit

You now have :你现在有:

*--X--*--*--Z------
    \           \  \
     \           B  F  # B: tip of backend, F: tip of frontend
      \
       *--*--*--T
  1. merge the two parts in feature :合并feature的两个部分:
$ git checkout feature
# have one developper fix conflicts on :
$ git merge backend
# and a second one on :
$ git merge frontend

Note : the merges can happen in any order.注意:合并可以按任何顺序发生。

The situation is now :现在的情况是:

*--X--*--*--Z------
    \           \  \
     \           B  F
      \           \  \
       *--*--*--T--*--M <- feature
  1. you can now merge feature into master您现在可以将feature合并到master

As for your workflows, if work on backend and frontend should be handled separately, you need to have some rule to reflect this in the branches managed by your developpers.至于你的工作流,如果backendfrontend应该分开处理,你需要有一些规则来反映你的开发人员管理的分支。

@bk2204 gives some directions towards doing this in his answer : keep work on backend separate from work on frontend, merge ASAP. @bk2204 在他的回答中给出了一些指导:将后端工作与前端工作分开,尽快合并。

There are a couple typical approaches to this problem:这个问题有几种典型的方法:

  • Merge changes frequently and use a feature-flagging system so that new changes can be integrated without becoming active.经常合并更改并使用功能标记系统,以便可以集成新更改而不会变为活动状态。 This lets individual developers work on different aspects of a feature and merge it incrementally, so a feature branch is limited to a single or a small number of developers who are capable of resolving conflicts individually.这让个别开发人员可以处理功能的不同方面并逐步合并它,因此功能分支仅限于能够单独解决冲突的单个或少数开发人员。
  • Make feature branches operate as a combination of individual developers' feature branches, and force developers to each rebase their own changes, resolving conflicts, and then re-integrate those individual developer branches back into the feature branch.使功能分支作为单个开发人员的功能分支的组合运行,并强制每个开发人员重新调整自己的更改,解决冲突,然后将这些单独的开发人员分支重新集成回功能分支。
  • Have developers from each of the areas pair together to work on conflict resolution.让每个领域的开发人员结对解决冲突。

I've personally experienced the first and last of these, and I prefer the former, but depending on your workflow, it may not be possible.我个人经历过第一个和最后一个,我更喜欢前者,但根据您的工作流程,这可能是不可能的。 For example, if you have multiple lines of development (eg, a maintenance and a development branch), the first solution won't solve all your problems, but it may reduce them.例如,如果您有多个开发线(例如,一个维护和一个开发分支),第一个解决方案不会解决您的所有问题,但可能会减少它们。

Generally, the more you can design your workflow such that the conflicts a developer has to solve are related to code they themselves are working on, the more successful you'll be.通常,您越能设计您的工作流程,使开发人员必须解决的冲突与他们自己正在处理的代码相关,您就会越成功。 Even if other developers in the same area are capable of resolving conflicts there, the developer working on a piece of code will be able to do it faster and be more likely to do it correctly because they're presently working there.即使同一区域的其他开发人员能够解决那里的冲突,处理一段代码的开发人员也能够更快地完成它,并且更有可能正确地完成它,因为他们目前正在那里工作。

A possible technical approach is using git imerge and stashing the incremental products somewhere as a ref, then having each developer resume the stashed state.一种可能的技术方法是使用git imerge并将增量产品存储在某处作为参考,然后让每个开发人员恢复隐藏状态。 This is likely to be tricky, though, and while possible, isn't recommended.不过,这可能很棘手,虽然可能,但不建议这样做。

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

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