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:
- Checkout
branch_A
, if not already, then pull fromremote_name/release_branch
intobranch_A
usinggit pull --no-rebase remote_name/release_branch
. This will create a merge commit, merging the latestrelease_branch
, including changes made bybranch_B
into your branch. If there are any conflicts, you will have to resolve them and perform a commit to complete the merge. - Checkout
branch_A
, fetchremote_name/release_branch
, then mergeremote_name/release_branch
intobranch_A
. This is basically the same as method 1, but broken into two steps. - Checkout
branch_A
, fetchremote_name/release_branch
, then rebasebranch_A
usingremote_name/release_branch
as the upstream branch. Alternatively, you can use usegit 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:
- git fetch colleague_nickname branch_B:branch_B
- git rebase branch_B
- git log // verify branch_B branch code added
- git push -f origin branch_A