owagh
owagh

Reputation: 3528

git rebase causes origin/master and master to diverge

I've already looked at questions

git rebase and git push: non-fast forward, why use?

and

master branch and 'origin/master' have diverged, how to 'undiverge' branches'?

I'm still not clear on what I need to do in this specific case. I had origin/master and master in sync first. The sequence of commits on both is

---o---A---B

I now commit two more commits on my local repository so that I have

---o---A---B---C---D

Now I did a rebase because I wanted to reorder the commits. So my master is now :-

---o---A---C---D---B

while my origin/master is still as it was in the first situation. Now, if I try to do a git push, it refuses to push it because master and origin/master have diverged. How do I push this rebase to origin?

Upvotes: 2

Views: 1667

Answers (2)

knice
knice

Reputation: 391

I've experienced this several times and at first I pulled like git said, which would try to merge my past changes with my new local changes -not okay. Basically, when your local repo branch has diverged from your remote branch, you must rebase your remote branch as well. E.g.:

git:(branch)$ git commit -m "my fix."
git:(branch)$ git checkout master
git:(master)$ git rebase branch
git:(master)$ git rebase origin/master
git:(master)$ git push origin master

great success!

Upvotes: 0

Ben Lee
Ben Lee

Reputation: 53309

You can force it with:

git push -f origin master

Be careful though. This is actually changing the history on origin, so only do this if you know it won't break anything for other devs, for example.

In general it's not a good idea to rebase commits that have already been pushed to a remote/central repository.

Upvotes: 2

Related Questions