Home > Enterprise >  GitLab merging changes from two branches
GitLab merging changes from two branches

Time:10-25

I have a current system of branches:

- deploy
- feature/1

I merged the branch feature/1 to deploy with selecting "squash commits", but didn't delete the branch itself yet. Then I created a new branch feature/2 from the feature/1 (meaning that feature/2 contains all the commits from feature/1). But now when I want to merge feature/2 to deploy and create merge request it shows that all the changes from feature/2 are conflicts and shows all the commits (with ones that were merged from feature/1), but I want it to be like in the first merge request, so it will show only the changes from feature/2 that weren't in feature/1 at the moment of the first merge to deploy.

I still have the merge request and the branch feature/1. How do I achieve something like this? There are two options available in the first merge request: "revert" and "cherry-pick", I read GitLab documentation on these buttons, but I'm still not sure what they do and what to pick in this situation. If I need to draw it, it looks something like this:

---deploy------ok-------------?
----feature/1--|              |
                ---feature/2--|

CodePudding user response:

The branch deploy already contains the changes of the feature/1. As the feature/2 also contains the same changes, you get the merge conflicts.

In general, if after merging feature/1 with deploy you create the feature/2 from the deploy instead of feature/1 the problem would not be there.

But since it's a bit late, I'd recommend the following easy approach:

  1. Create another branch from the deploy, let's call it feature/2.2.
  2. Cherry pick all the new commits of the feature/2 (that are not part of feature/1) to the new branch.

Then you'll be able to easily merge the new branch with deploy.


Details:
Here's what it looks like in your current case:

deploy    => d1-d2-d3-f'------------f1-f2-f3-ff1-ff2-ff3
                     /             /?
feature/1 => f1-f2-f3             /?
                    |            /?  
feature/2 => f1-f2-f3-ff1-ff2-ff3

The branch deploy already contains the commit f' which is the squashed commit of f1-f2-f3.
So, if you try to merge feature/2 with deploy, git will try to put the f1-f2-f3-ff1-ff2-ff3 above the f' which already has the changes f1-f2-f3. And this is where you get merge conflicts.

Thus, to solve the problem we just create a new branch from deploy instead, so the f1-f2-f3 are not there anymore, and just cherry-pick the other ff1-ff2-ff3 which won't have merge conflicts. And then we can merge them back to deploy like this (by squashing ff1-ff2-ff3 into ff'):

deploy      => d1-d2-d3-f'------------ff'
                        |             /
feature/2.2 => d1-d2-d3-f'-ff1-ff2-ff3
                          /   /    /
feature/2   => f1-f2-f3-ff1-ff2-ff3
                      |               
feature/1   => f1-f2-f3                   
  • Related