Home > database >  How to take changes from one remote branch to other remote branch
How to take changes from one remote branch to other remote branch

Time:05-07

I'm working on a project and I cut a branch from a release_branch let's call my branch branch_A. My colleague also had cut another branch from same release branch and lets call that branch_B. We are working on the same file and section hence to avoid any merge conflicts I wanted to take changes from branch_B to my branch_A. Changes of branch_B has already been merged into that release_branch from which we had cut our branches earlier.

Can somebody tell what should I do? I've tried pull but it didn't work. Also I switched to the release_branch and took pull and changes were there but as soon as I switched to my branch_A the changes were gone. Please help.

CodePudding user response:

First, make sure that you commit all changes to your branch_A and if you are concerned about losing your work as part of the process, create a new branch at branch_A (e.g. branch_A_old).

Since you will eventually be merging back to release_branch, you can use one of the following approaches:

  1. Checkout branch_A, if not already, then pull from remote_name/release_branch into branch_A using git pull --no-rebase remote_name/release_branch. This will create a merge commit, merging the latest release_branch, including changes made by branch_B into your branch. If there are any conflicts, you will have to resolve them and perform a commit to complete the merge.
  2. Checkout branch_A, fetch remote_name/release_branch, then merge remote_name/release_branch into branch_A. This is basically the same as method 1, but broken into two steps.
  3. Checkout branch_A, fetch remote_name/release_branch, then rebase branch_A using remote_name/release_branch as the upstream branch. Alternatively, you can use use git pull --rebase remote_name/release_branch to combine the fetch and rebase into one step.

Methods 1 and 2 will keep the merge as part of the history and method 3 will update all of the commits in your branch so that your branch begins at the latest commit of release_branch.

Methods 1 and 2 are usually easier, although this is debatable, but method 3 will provide a cleaner history. If you do use method 1 or 2, you can always perform a squash later to clean up the history.

CodePudding user response:

Rebase:

Add your colleague's remote

git remote add <colleague_nickname> git@;

Check remote is added or not

git remote -v

Steps:

  1. git fetch colleague_nickname branch_B:branch_B
  2. git rebase branch_B
  3. git log // verify branch_B branch code added
  4. git push -f origin branch_A
  • Related