Git: Pulling a rebased branch

Itai Hanski picture Itai Hanski · May 27, 2013 · Viewed 25.7k times · Source

Let me describe my situation:

Mr Blond and Mr Orange are working on branch A that branches out of the master branch on commit M1. Branch A has 2 commits: A1 and A2.

M1  
   \
    \
     A1 - A2

Meanwhile, Mr Orange committed and pushed 2 more commits on the master branch, M2 and M3.

M1  - M2 - M3
   \
    \
     A1 - A2

Mr Blond pulls from the remote, and after a while decides to rebase onto the master branch:

M1  - M2 - M3
   \         \
    \         \
     A1 - A2   A1` - A2`

Now A1` and A2` are the rebased commits that exist locally at Mr blond's, and A1 and A2 exist remotely. Mr Blond pushes his commits, using -f to force his changes and "rewrite" history. Now the remote repository looks like this:

M1  - M2 - M3
             \
              \
               A1` - A2`

But Mr. Orange worked on the A branch as well. His local repository still looks like this:

M1  - M2 - M3
   \
    \
     A1 - A2

What does Mr. Orange need to do in order to be synchronized with A branch in the remote repository?

A normal pull won't work. Will pull -f force the changes from the remote locally? I know that deleting the local version of A and bringing it again from the remote repository will do the trick but that doesn't seem to be a good way to achieve that.

Answer

Gary Fixler picture Gary Fixler · May 27, 2013

If Mr. Orange doesn't mind losing his changes, he can fetch from the server, then git checkout A to get onto his local A branch, then (presuming the remote is named "origin") git reset --hard origin/A to reset his A to where the remote's A is.

If he is concerned with losing changes, he can merge the server's changes in to resolve them (from his own A branch, and presuming again that the remote is named "origin") with git merge origin/A. This will make a new commit that's on top of both his and the remote's A branches with the changes from both merged together. Then this can be pushed back to the remote.