简体   繁体   English

如何最小化git合并冲突?

[英]How to minimize git merge conflicts?

Our company uses git to to keep track of document/code changes, with several people making changes and pushing to a central repo. 我们公司使用git来跟踪文档/代码的变化,有几个人进行更改并推送到中央仓库。 Most of them are novices when it comes to git or command line tools in general. 一般来说,大多数人都是git或命令行工具的新手。 For them we have scripts to update or publish their local repo after making changes. 对于他们,我们有脚本在更改后更新或发布他们的本地仓库。

Are there any workflows that work better for situations like these, to minimize merge conflicts occurring that have to be sorted out by someone more experienced with git? 是否有任何工作流程可以更好地适应这些情况,以最大限度地减少必须由更有经验的git的人整理的合并冲突?

Git is not a replacement for proper development practices. Git不是正确开发实践的替代品。 It will not code better for you. 它不会为您编码更好

If two developers are touching the exact same code, git has no way of making their job easier, if it's a merge conflict, it'll be one no matter if you're on a branch or not. 如果两个开发人员触摸完全相同的代码,git无法使他们的工作更容易,如果它是合并冲突,无论你是否在分支机构,它都将是一个。

Short term solution: use proper local and remote branches to encapsulate work on different features. 短期解决方案:使用适当的本地和远程分支来封装不同功能的工作。 Use branch diffs (or github pull requests) to review feature sets and help go over diffs and conflicts. 使用分支差异(或github拉取请求)来查看功能集并帮助检查差异和冲突。

Long term: fix your code, adapt your it to your team and vice versa, and use proper development practices. 长期:修复代码,使其适应您的团队,反之亦然,并使用适当的开发实践。

The number one reason for merge conflicts is the time between each merge . 合并冲突的首要原因是每次合并之间时间
The longer you wait between each merge, the surer you get to see merge conflicts. 每次合并之间等待的时间越长,您就越可以看到合并冲突。

You can minimize that by choosing a merge workflow (like git flow for instance) which will advocate for branch per feature, and facilitate the isolation of tasks. 您可以通过选择合并工作流 (例如git flow )来最小化它,该工作 将提倡每个功能的分支,并促进任务的隔离。
But as long as a common set of files is involved (in two different developments), you will end up with merge conflicts, especially if you wait too long. 但只要涉及一组通用文件(在两个不同的开发中),您最终会遇到合并冲突,尤其是等待时间过长的情况。

So with a distributed VCS, learn to publish (push/pull) regularly, and learn to rebase before merging . 因此,使用分布式 VCS,学习定期发布(推/拉),并在合并之前学习重组

Not only will that decrease the number of conflicts, it will also reduce the number of semantic conflicts : those are merges which seem automatic (no conflicts), but produce an incorrect code. 这不仅可以减少冲突的数量,还可以减少语义冲突的数量:这些是合并似乎是自动的(没有冲突),但会产生错误的代码。
See " Better, simpler example of 'semantic conflict'? ". 请参阅“ 更好,更简单的'语义冲突'示例? ”。

Nothing magical. 没有什么神奇的。 Mostly discipline. 主要是纪律。

If people have to do something like pull requests or to wait for commits to be reviewed before being merged with some "main" repo or branch, do try to make the time for review as short as possible. 如果人们在与某些“主要”回购或分支合并之前必须做拉动请求或等待提交被审查之类的事情,请尽量缩短审查时间。

You should probably try to make branch lifetime short, but that really depends on the kind of project you are working on. 您可能应该尝试缩短分支生命周期,但这实际上取决于您正在处理的项目类型。

Make it a rule that all commits must be as small as possible. 规定所有提交必须尽可能小。 Make it a rule that commits must change only one thing. 制定一项规则,即提交必须只改变一件事。 Make it a rule not to mix cosmetic changes (like whitespace) with functional changes and significant refactoring. 不要将化妆品变化(如空白)与功能变化和重大重构混合在一起。 (Don't forbid whitespace changes completely - just separate them from other changes.) (不要完全禁止空格更改 - 只需将它们与其他更改分开。)

Beyond version control itself, try to assign tasks to coders in a way that diminishes the chance that they change the same code on the same week or so. 除了版本控制本身之外,尝试以减少他们在同一周左右更改相同代码的可能性的方式将任务分配给编码人员。

There's an article written here that will help you understand better, " A successful Git branching model ". 这里写的一篇文章将帮助您更好地理解“ 成功的Git分支模型 ”。 It states that you should separate different types of branches, which include: 它声明你应该分开不同类型的分支,包括:

  • master
  • hotfixes 修补程序
  • release branches 发布分支机构
  • develop 开发
  • feature branches 特色分支

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

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