Home > Net >  Merge branches and Fix conflicts
Merge branches and Fix conflicts

Time:12-15

I have two branches I wanna merge them into each other but the problem is that one of them stopped taking any update from the other months ago. So, when I try to merge them I get a large number of complicated conflicts.

Is there any solution to merge them safely?

Note: I'm using Android Studio

CodePudding user response:

There is no way around this.... conflicts happen because code went in different directions and so they have to dealt with. One way you could turn a big pile of conflicts to be dealt with in a single shot could be to rebase the longer branch (since they diverged) onto each one of the commits of the shorter branch. Say.... you have these two branches

A <- B <- C <- D <- E <- (branch1)
     ^
      \- F <- G <- (branch2)

You could run git switch branch2; git rebase C. You might get smaller conflicts, and end up with this:

A <- B <- C <- D <- E <- (branch1)
          ^
           \- F' <- G' <- (branch2)

Then git rebase D, and end up with this:

A <- B <- C <- D <- E <- (branch1)
               ^
                \- F'' <- G'' <- (branch2)

And finally git rebase branch1 and end up with this:

A <- B <- C <- D <- E <- (branch1)
                    ^
                     \- F''' <- G''' <- (branch2)

But this does not mean you will avoid conflicts..... you could actually get more conflicts along the way, but they should theoretically be smaller (that might actually not be the case, but alas, it's impossible to know beforehand).

Another approach that would work is by merging but not the tips, but halfway or in segments.... then conflicts should be smaller... and in the end, when you have your final desired tree when you finally merge the real tip of the branch you really want, you can run a git commit-tree command to create a commit "out of thin air" that uses the real 2 branches as parents and the final tree of all the merges you created so that you avoid having all those merges that you tried in the middle (in case you want to avoid them fro showing up).

  • Related