简体   繁体   English

Git 合并发布分支因小版本更改而发生冲突

[英]Git merging release branches conflicts because of minor version changes

For a project, say I have some release branches:对于一个项目,假设我有一些发布分支:

release/1.2
release/1.1
release/1.0

These release branches are based on the minor version.这些发布分支基于次要版本。 Because they keep being supported, each of them will be at a different patch version, stored somewhere in some build configuration file (gradle in my case).因为它们一直受到支持,所以它们中的每一个都将处于不同的补丁版本,存储在某个构建配置文件(在我的情况下为 gradle)中的某处。 For example:例如:

release/1.2
  build.gradle contains "version 1.2.1"
release/1.1
  build.gradle contains "version 1.1.4"
release/1.0
  build.gradle contains "version 1.0.7"

Now, suppose I fix a bug in branch release/1.0 .现在,假设我修复了分支release/1.0一个错误。 Then I'd like to merge this branch to the next two, to propagate the fix.然后我想将此分支合并到接下来的两个分支,以传播修复程序。 In general, this would work quite smoothly.一般来说,这会很顺利。

However, when fixing the bug in release/1.0 I would also want to bump its patch version, from 1.0.7 to 1.0.8, changing the file build.gradle to contain "version 1.0.8".但是,在修复release/1.0的错误时,我还想将其补丁版本从 1.0.7 提高到 1.0.8,将文件 build.gradle 更改为包含“版本 1.0.8”。 But if I do that, the merge into the next release branches will create a conflict on that file.但是如果我这样做,合并到下一个发布分支将在该文件上产生冲突。

Obviously there must be a better way to support my initial intentions.显然必须有更好的方法来支持我的最初意图。 What are the best practices in this case?在这种情况下,最佳做法是什么?

One way I could think of is to not store the full version in a file, but only use git tags, and then have gradle (or any other build tool) obtain the full version from the git tag.我能想到的一种方法是不将完整版本存储在文件中,而只使用 git 标签,然后让 gradle(或任何其他构建工具)从 git 标签中获取完整版本。 One major disadvantage of this approach is that the build would fail on a sourcebase that is exported from git.这种方法的一个主要缺点是构建将在从 git 导出的源库上失败。

Two possible solutions come to mind:想到了两种可能的解决方案:

Cherry-picking采摘樱桃

I have found that instead of merging, git cherry-pick works a lot better.我发现git cherry-pick比合并要好得多。 With merging, you're running into the issue you are describing, and the version number might only be one instance of this.通过合并,您遇到了您所描述的问题,而版本号可能只是其中的一个实例。 As the branches diverge further, you will find other conflicts that will not be easy to resolve.随着分支的进一步分歧,您会发现其他不容易解决的冲突。 Since merging will always try to merge from the point where the branches were created, in many cases, far too many changes are pulled in.由于合并总是尝试从创建分支的点开始合并,因此在许多情况下,引入了太多的更改。

With cherry-picking, you can migrate individual commits from one branch to another one.使用cherry-picking,您可以将单个提交从一个分支迁移到另一个分支。 This works a lot better, since you can pick and choose which parts you want to migrate.这样效果会更好,因为您可以挑选要迁移的部分。

git cherry-pick 1234567

This will take the commit identified by the mentioned hash (it can be on any branch, in your case on release/1.0 ) and apply the commit on your current branch.这将采用由提到的哈希标识的提交(它可以在任何分支上,在您的情况下为release/1.0 )并将提交应用到您当前的分支上。 It will just include the changes from this one commit, nothing else.它将只包含来自此一次提交的更改,仅包含其他内容。

For me, this works a lot better than merging between support branches, since I can just take the parts that I want to fix, and does not include any other changes from previous or unrelated commits.对我来说,这比在支持分支之间合并要好得多,因为我可以只获取我想要修复的部分,并且不包括来自先前或不相关提交的任何其他更改。

Git Attributes Git 属性

If cherry-picking is not what you're looking for, you can use Git Attributes to define a merge-strategy for a specific file or a set of files.如果挑选不是你想要的,你可以使用 Git 属性来定义一个特定文件或一组文件的合并策略

See here for more details.请参阅此处了解更多详情。

It lets you define an attribute like the following, where for the database.xml file, always our side is used when merging, disallowing any incoming changes during a merge for this file:它允许您定义如下所示的属性,其中对于database.xml文件,合并时始终使用我们的一侧,不允许在合并期间对该文件进行任何传入更改:

database.xml merge=ours

You would define the above in a .gitattributes file in your repo.您将在您的存储库中的.gitattributes文件中定义上述内容。

In your case, you would probably use something like在您的情况下,您可能会使用类似的东西

build.gradle merge=ours

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

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