Question

I have two remote git branches (call them "RemoteA" and "RemoteB").

"RemoteB" was branched off of "RemoteA" some time in the past. After that point many commits have been applied to both branches but I now want "RemoteB" to pick up all of the commits in "RemoteA".

A-B-C-D-E  (RemoteA)
 \
  F-G-H-I  (RemoteB)

One idea I had was to attempt to rebase "RemoteB" based off of the latest tag in "RemoteA", but I've seen some community posts advising against that. Is this possible (any sample commands would be appreciated) and if so is it something I should even attempt?

A-B-C-D-E  (RemoteA)
         \
          F-G-H-I  (RemoteB)

Another idea was to create a new branch off of "RemoteA", call it "RemoteC" and manually cherry pick changes from "RemoteB" onto it. What are the best practices for this type of situation?

Was it helpful?

Solution

Since these are remote branches (and assuming that you are not working in isolation), it is a very bad idea to rebase one onto the other.

This is because rebasing "B" onto "A" will recreate the entire history of "B". Anybody else who will retrieve the remote "B" will now have to deal with that (usually that means they are seeing conflicting changes). You will be making live miserable for your collaborators.

To pick up the changes from "A" and incorporate them into "B", checkout your local version of "B", and merge "A" into it. Then push the updated "B" to the remote repository.

As a general rule: do not rebase branches that are already in a remote repository, unless you're the only person working with that remote.

OTHER TIPS

What are the best practices for this type of situation?

That mainly depends on whether there is a chance that other people already have seen the commits in both branches.

  • If one branch—say, RemoteB—is on your local machine and has never been pushed to a public repository, you can safely rebase it on top of the other branch:

    git checkout RemoteB
    git rebase RemoteA
    
  • Otherwise, the usual method is to merge one branch into the other:

    git checkout RemoteB
    git merge RemoteA
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top