简体   繁体   English

在多个分支上更改文件时,如何正确合并git分支?

[英]How to merge a git branch properly when a file is changed on multiple branches?

We are working on a file on different branches. 我们正在不同分支上处理文件。

After merging, the final code becomes erroneous, even though it works fine on each of the branches before merging. 合并后,即使最终代码在合并之前在每个分支上都能正常工作,最终代码也会变得错误。

I'm using the accepted answer given here as a branching/merging strategy: https://stackoverflow.com/a/5602109/4746692 . 我使用此处给出的可接受答案作为分支/合并策略: https : //stackoverflow.com/a/5602109/4746692

Is there a step that I'm missing? 我缺少一个步骤吗?

I have file on master branch. 我在master分支上有文件。 Sample code 样例代码

public class Test {
  public static void main(String[] args) {
    int a = 1;
    int b = 2;
    System.out.println(b);
  }
}

I branch out from here to newBranch. 我从这里分支到newBranch。

git checkout -b newBranch

On the newBranch, I edit the code to print the value of a . 在newBranch,我编辑的代码打印值。

public class Test {
  public static void main(String[] args) {
    int a = 1;
    int b = 2;
    System.out.println(b);
    System.out.println(a);
  }
}

While on master, some other developer removes the statement declaring the variable a . 在主服务器上时,其他一些开发人员将删除声明变量a的语句。

git checkout master
public class Test {
  public static void main(String[] args) {
    int b = 2;
    System.out.println(b);
  }
}

When I merge the newBranch back to master, the declaration of variable a will be missing, and the print statement will throw an error. 当我将newBranch合并回master时,变量a的声明将丢失,并且print语句将引发错误。 Note that this merging doesn't give any merge conflict. 请注意,这种合并不会产生任何合并冲突。

git checkout master
git pull origin master
git merge --no-ff newBranch

The merged code looks like 合并的代码看起来像

public class Test {
  public static void main(String[] args) {
    int b = 2;
    System.out.println(b);
    System.out.println(a);
  }
}

Is it a good practice to pull master into newBranch and test before merging it back to master? 将master纳入newBranch并进行测试,然后再将其合并回master是一种好习惯吗?

"Is it a good practice to pull master into newBranch and test before merging it back to master?" “将master纳入newBranch并进行测试,然后再将其合并回master是一种好习惯吗?”

Yes. 是。 And in this case, it makes perfect sense that the code doesn't work afterwards. 在这种情况下,完全合理的是,此后代码将无法工作。 After all, somebody removed some code, and you added a piece of code. 毕竟,有人删除了一些代码,然后您添加了一段代码。 The changes are not in the same locations, so there is no conflict. 更改不在同一位置,因此没有冲突。 Git is not a compiler and is not aware of the relation between the changes, so both changes are applied, and you'll have to solve this manually. Git不是编译器,并且不知道更改之间的关系,因此两个更改都被应用,因此您必须手动解决。

You can always use the git rerere to apply the merge results to all the givenm branches. 您始终可以使用git rerere将合并结果应用于所有给定的分支。

git rerere simply record the way you have resolved the conflict and then when the same conflicts appear on different branches it will simply apply the patch you created to resolve all the conflicts. git rerere只需记录您解决冲突的方式,然后当相同的冲突出现在不同的分支上时,它将简单地应用您创建的补丁来解决所有冲突。

Read more 阅读更多

git rerere
Fix conflicts only once with git rerere 用git rerere修复冲突一次

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

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