Git branch diverged after rebase

Solution 1:

When you rebase a branch, you have to rewrite the commits for any commit which is above the commits in the branch onto which you are rebasing. This is because one of the properties of a commit is its parent (or parents). When you rebase, you're changing the parent of the oldest local commit on your branch - and thus changing the commit hashes of all of your local commits, since this change bubbles up through the commits transitively.

Since you'd already pushed the branch, you should have merged in the source branch, rather than rebasing against it. It is possible to "force push" your new branch (using the -f flag), but a normal push won't work, because the integrity of the branches history will be disturbed. If you are collaborating with others on this branch, force pushing is a bad idea, as it will cause other collaborators to become very confused when their history suddenly doesn't match.

TL;DR - If you're not collaborating, push the branch using push -f. If you are, reset the branch to the previous state, and merge in the source branch, instead.

Solution 2:

All your commits have changed ids, so the diversion is not truly a diverge.

To get your problem resolved you have to overwrite your remote branch:

git push -f origin experiment

http://git-scm.com/book/ch3-6.html

Explanation:

See how in this image C3 is not put as C3 after the rebase, but as C3'. This is because it is not exactly C3, but it has all of its code changes.

Rebase

On this other image you get the picture of what a rebase is seen when a remote is involved, and why there is a diversion.

diverge and git push

In any case, after you do the forced push, it will tell you that it did a (force update), you should be fine at that point.

Checkout the link at the top, and search for "git push --force". You will see a more detailed explanation.

Solution 3:

I had success with the rebase diverge for a push by doing the following:

git checkout mybranch
git pull
git push origin mybranch

The pull resolved the diverge.

BEFORE the pull

Your branch and 'origin/mybranch' have diverged,
and have 2 and 1 different commit(s) each, respectively.

PULL output

Merge made by recursive. mypath/myfile.py | 12 +++++++++++- 1 files changed, 11 insertions(+), 1 deletions(-)

AFTER pull

Your branch is ahead of 'origin/mybranch' by 3 commits.

AFTER PUSH

mybranch is 3 ahead of the branch the still has an open pull request merge message added to the commit history Merge branch mybranch of remote into mybranch

I am assuming this is probably what the force push does, and I have not verified.

As the others have said, avoid a rebase if you already have an open pull request. I am providing this example as something that worked for me.