git: branches diverged; how to proceed?

70,528

Solution 1

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

Solution 2

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?

Solution 3

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'.

Share:
70,528
sds
Author by

sds

Math, Data Science, History...

Updated on April 07, 2020

Comments

  • sds
    sds about 4 years

    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.

  • Dean Burge
    Dean Burge over 12 years
    Git is pretty good at merging (or, automatically resolving "conflicts") and re-applying patches, but alas, sometimes it needs your help. In these cases, in my experience, I know of no other tool who'd do any better. You really need to manually resolve these conflicts.
  • Pat Notz
    Pat Notz over 12 years
    Good point -- be sure to check out git rerere.
  • Pat Notz
    Pat Notz over 12 years
    In place of "@{upstream}" you can even use "@{u}".
  • sds
    sds over 12 years
    This does not answer the question.
  • Philipp Claßen
    Philipp Claßen over 11 years
    @sds I think it does. After you merged the branches, solved the merge conflicts, and replayed your changes on top of the master, you are back to normal. This is what wilhelmtell described.
  • Cocoadelica
    Cocoadelica over 11 years
    Upvoted. I had the exact same issue and this fixed it. Personally I don't like vi or vim so I changed my default editor to nano. Google for instructions.
  • hauron
    hauron almost 10 years
    This is amazingly efficient and clean - you end up with a linear history.
  • diyism
    diyism almost 9 years
    when "git pull --rebase", it says "error: feeding unmodified ... to diffcore Cannot pull with rebase: Your index contains uncommitted changes. Please commit or stash them", but when "git commit", it says "Your branch and 'origin/master' have diverged, and have 1 and 1 different commit each, respectively. (use "git pull" to merge the remote branch into yours) nothing to commit, working directory clean"
  • learning2learn
    learning2learn over 2 years
    I haven't tried this. Can someone explain more why it would work?
  • dmcontador
    dmcontador about 2 years
    Beware: git reset --hard will delete unstaged modifications. Maybe it is prefearable to stash rather than branch the current workspace.