2

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?

Community
  • 1
  • 1
owagh
  • 3,428
  • 2
  • 31
  • 53

2 Answers2

2

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.

Ben Lee
  • 52,489
  • 13
  • 125
  • 145
  • Hmmm. Would it be safer/easier if I pushed my changes in state 2 to the origin then ssh-ed to the remote machine and did the rebase in the remote repository and then did a git pull on my local machine? Or is this strictly worse that what I'm trying to do? – owagh Apr 10 '12 at 23:30
  • @owagh, that's a roundabout method for the same effect and has the same problem -- changing history on the remote repo could cause problems for other developers using it. – Ben Lee Apr 10 '12 at 23:32
  • Ohk. Lemme try it out and see what happens. – owagh Apr 10 '12 at 23:34
0

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!

knice
  • 391
  • 2
  • 8