简体   繁体   中英

git: branches diverged; how to proceed?

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.

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

Have you tried git merge origin/master ?

Your remote changes are stored in the branch origin/master . (Or it will, if you do git fetch .) Just merge the two branches - master and origin/master - like any two branches and resolve the conflicts (if any).

This may help you if you need to know how to resolve git conflicts.

How to resolve merge conflicts in Git?

Git says it tried to do exactly what you want (re-apply your patches on top of the newest changes from origin/master) but failed with a conflict. Right after git pull --rebase conflicts open an editor with the conflicted files ( git status will list these under "both changed") and resolve the conflicts, marked in standard diff lingua. When you're done resolving the conflict throw in a git rebase --continue (or git rebase --skip if your resolution introduces no changes).

Read about his at Stackexchange documentation for 'Resolving merge conflicts after a Git rebase' .

Here are some good answers to the same problem (only without conflict resolving):

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

First, you may want to review what has been changed on the remote master in comparison to your local version:

git log HEAD..origin/master

To fix your actually problem, it boils down to the same line that wilhelmtell proposed:

git pull --rebase

As you said, you will get conflicts.

Conflicts resolving is a recurring problem. If you haven't done it yet, you may take a look at git mergetool (see git help mergetool for details). To get graphical support, I would recommend to overwrite the merge.tool configuration. For example, if you want to use meld for 3-way merges, you can use:

git config --global merge.tool meld

So after you resolved the conflict, what has git pull --rebase done? It merged all changes from the origin/master into your local master, and replayed your changes on top of it. Congratulations, you are back to normal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM