Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Pulling a rebased branch

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.

like image 344
Itai Hanski Avatar asked May 27 '13 12:05

Itai Hanski


People also ask

How do I pull a rebase in git?

first, you have to sync the local branch on your PC with the remote branch. In this case git pull --rebase works like magic. After git pull --rebase your local branch and remote branch have same history with the same commit ids. Then now if you add a new commit/changes to the PR branch.

Do you pull after a rebase?

There is no need to do a git pull after you have rebased your feature branch on top of master . With your current workflow, the reason why git status is telling you this: Your branch and 'origin/feature' have diverged, and have 27 and 2 different commits each, respectively.

Should I git pull rebase?

It is best practice to always rebase your local commits when you pull before pushing them. As nobody knows your commits yet, nobody will be confused when they are rebased but the additional commit of a merge would be unnecessarily confusing.

What is rebase local branch when pulling?

A merge commit is created to point to the latest local and remote commits. In case of a rebase, we use the command git pull --rebase . In a rebase, the unpublished local changes of the local branch are reapplied on top of the published changes of the remote repository.


1 Answers

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.

like image 153
Gary Fixler Avatar answered Sep 21 '22 09:09

Gary Fixler