git: branches diverged; how to proceed?

There are, of course, several ways you could do this manually. You’ll still have the same conflicts because git is basically doing this for you under the hood. But if you want to do this manually, here are a couple of ways.

First, export your commits as a series of patches. The easiest way to do this is using git format-patch:

git format-patch -M @{upstream}

will produce 7 patch files — one for each of your commits. (Note that “@{upstream}” is literal — it’s a not so well known feature of git.) This is better than capturing the output of git diff because all of the commit information (author, date, message, etc.) are preserved.

Then you could reset your repository to match the upstream:

git reset --hard @{upstream}

Then you can re-apply your patches using git am — either one at a time or all at once.

git am 0001-blah-blah.patch
git am 0002-blah-blah.patch
...

A second option would be to create a spare branch with your work on it:

git branch scrap

Then reset your branch to the upstream:

git reset --hard @{upstream}

Then cherry-pick the commits over:

git cherry-pick scrap~6
git cherry-pick scrap~5
git cherry-pick scrap~4
...

Then trash the scrap branch:

git branch -D scrap

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)