Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git: branches diverged; how to proceed?

Tags:

git

git-rebase

My local tree has diverged from the master:

$ git status # On branch master # Your branch and 'origin/master' have diverged, # and have 7 and 3 different commit(s) each, respectively. # nothing to commit (working directory clean) 

I tried git pull --rebase and failed:

$ git pull --rebase First, rewinding head to replay your work on top of it... Applying: * ... Using index info to reconstruct a base tree... Falling back to patching base and 3-way merge... Auto-merging ChangeLog CONFLICT (content): Merge conflict in ChangeLog Failed to merge in the changes. Patch failed at 0001 * ... 

So I reverted with git rebase --abort and I am now at square 1.

What I want is:

  1. "Export" my 7 patches into human readable diff files (a la hg export).
  2. Make my tree a carbon copy of the origin/master (a la hg strip).
  3. re-apply my 7 patches one-by-one by hand (a la hg import).

I do understand that git rebase --continue does this. I did it and it did work (after a few manual merges and a git add). However, I want to be able to do that manually, so I am wondering what are the git commands corresponding to the hg commands above.

Thanks.

PS. Please do not tell me that using a ChangeLog file with git is stupid. Even if it is, it is not up to me.

like image 652
sds Avatar asked Jan 03 '12 17:01

sds


People also ask

What does it mean when your branch has diverged?

Occasionally, you or your team members may run into a diverged branch. A diverged branch means the data you're looking at locally isn't the same as what is currently stored on our servers.

How do you reconcile divergent branches in git settings?

Solution 1: Switch to Merge Strategy The default Git behavior is merging, which will create a new commit on your local branch that resolve those changes. You can use git config pull. rebase false command to switch back to default merging strategy instead of using rebasing strategy.


1 Answers

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 
like image 171
Pat Notz Avatar answered Sep 23 '22 23:09

Pat Notz