saurav
saurav

Reputation: 369

How to take changes from one git branch to another branch

I have the below branches.

Branch A : The main branch or the master branch.

Branch B : Created from A.

Branch C : Created from B.

Branch D : Created from A

Now I did some changes to branch C and now I could merge them to branch B. But at this moment a new branch D is created from branch A and now I should merge my changes to branch D. How to take all of my changes to branch D ? Since I have lots of commits I can not cherry-pick them one by one. I tried to rebase branch D on branch C, but I am getting lots of conflicts which I can't resolve manually. So what is the best way to merge branch C to branch D ?

EDIT 1 :

Let me clarify my problem a bit more. Here branch A is the master branch. Branch B is a release branch (which has just ended, so the branch is now in active). Branch C is my topic branch cut from B, branch D is the new release branch cut from A. Ideally we should merge our topic branch only to the release branch and cherry pick those changes to the master branch (A).So we create a topic branch from the current release branch then do our changes and raise merge request. We should not add anything to the inactive release branch. Now I was working on a branch C which was cut from a release branch B which has become inactive. Now I should create a branch from D and add all my changes there and raise one Merge Request. I don't want to do that. I want to use my branch C to raise a Merge Request on branch D which will not have any conflict. Is it possible ?

My solution is :

I would create a temporary branch B_temp out of branch B. Squash merge my topic branch C to B_temp. Then create a new branch E out of D, cherry pick the commit from B_temp to branch E, resolve any merge conflict (I can resolve those as they are my changes only). Raise merge request for F onto branch D. I am wondering if there is any better solution available instead of creating new branches and cherry picking if I could simply use the old branch C.

Note : We are not allowed to merge anything in the master and release branches without proper Merge Request and approval.

Upvotes: 0

Views: 1701

Answers (1)

Marina Liu
Marina Liu

Reputation: 38106

Assume the commit history for all branches as below:

...---A---...---G   branchA, branchD
       \
        B---C---D---M   branchB
             \     /
              E---F branchC

There usually has two ways to merge changes from branchC to branchD.

Option 1: merge branchB into branchD

Since you have merged branchC into branchB, so you can merge branchB into branchD, and then the changes from branchC will be applied to branchD:

git checkout branchD
git merge branchB

Then the commit history will be:

             branchA
                |
...---A---...---G-----H   branchD
       \             /
        B---C---D---M   branchB
             \     /
              E---F branchC

Option 2: rebase branchC onto branchA, then merge into branchD

Or you can rebase branchC onto branchA firstly by:

git rebase --onto branchA branchB branchC

And the commit history will be:

                  E'---F' branchC
                 /
...---A---...---G    branchA, branchD
       \
        B---C---D---M   branchB
             \     /
              E---F 

Then merge the rebased branchC into branchD:

git checkout branchD
git merge branchC

And the commit history will be:

              branchA
                |
...---A---...---G---E'---F'    branchC, branchD
       \
        B---C---D---M   branchB
             \     /
              E---F 

I want to use my branch C to raise a Merge Request on branch D which will not have any conflict. Is it possible ?

Yes, it possible. First, please check if there has file(s) which was changes both in branchC and branchD by:

git diff branchC branchD --name-only
  • If there has no files changed in both sides, then create a merge request to merge branchC into branchD directly.
  • Else, overwrite the conflict file version in branchC by using branchD's file version:

    git checkout branchC
    git checkout branchD -- filename
    git commit -m 'overwrite the filename by using the version from branchD'
    git push origin branchC
    

    Then create a merge request to merge branchC into branchD, and there won't has merge confilcts.

Upvotes: 1

Related Questions