Git fetch change from branch A to branch B on local

Viewed 189

I struggle a bit doing a thing that I am sure is super easy.

Here is my typical git history:

     C--F   <-- [Branch B] The branch I am working on
    /
A--B--D--E  <-- [Branch A] Another branch where work is done

A is the past and F the present.

Before push/pull-request on Branch B, I want to be able to pull the last change E on Branch A into my local that is on Branch B.

Ideally, I would like the rebase of Branch A in Branch B to be a new commit on Branch B. So I can easily revert is the two features are not compatible. This commit should only contain the necessary changes for Branch B to be up to date with Branch A. Thus, in case the two features are not compatible, it is super easy to roll back.

The changes made on Branch A and Branch B are not on the same file. If it happens they are, I choose how to manage the conflict (--rebase).

Do you have any ideas on how to do this?

Thanks all


[Additional Information]

I tried to pull --all and/or checkout on Branch A.

However, when I checkout back in Branch B and pull I received the the message Current branch Branch B is up to date.

Indeed the base -step B- hasn't changed and the last change -step F- are already on my local.

However, what I what is to merge without merging. I don't want the changes on Branch B to go on Branch A. I want a step G in Branch B with the latest change of Branch A -step E- and the latest change of Branch B -step F-.

2 Answers

If you only want to have commit "E", you can cherry-pick it:

git checkout branch-b
git cherry-pick <E commit SHA>

Otherwise, if you want to merge all commits, you can rebase/merge branch branch-a into branch branch-b:

git checkout branch-b
git fetch && git rebase origin/branch-a

As @mousetail mentioned, while on branch B, do git fetch && git merge origin/A. Note that merging is not symmetrical. As mentioned in the docs:

Incorporates changes from the named commits (since the time their histories diverged from the current branch) into the current branch.

It only changes the current branch you are on (your branch B in this case) and leaves A as it is. When merge conflicts arise, you can address them locally with additional commits. When merging is finished you are safe to merge B back into A without conflicts. This is a valid strategy to resolve merge conflicts with a protected branch (for example if A was protected).

Related