gitgithubmergegit-am

Git: Merging two repo with history having conflits


Having the Project which is having around 133 commits in November I had cloned the project and created a new repo before 50 commits that means:

A----B----C----D----E
          Cloned New Repo from C
           \__ C

Now there are about 15-20 commits in the new repo, but no commit is pushed to old repo but while coding many files and internally changed.

A----B----C----D----E
          Cloned New Repo from C
           \__ C----C1----C2---C3---C4

Now I want to merge these two repo into one as:

A----B----C----D----E----C1----C2---C3---C4

After searching internet got some commands and tried to do merging with following:

 git --git-dir=../<some directory>/.git|
    format-patch -k -15 --ignore-space-at-eol --stdout <commit SHA>|
    git am -k -3

It gives the confilts with First commit and had sucessfully resolved it :

 git mergetool
 gm am --continue

shows an error:

Did you hand edit your patch?
It does not apply to blobs recorded in its index.
Cannot fall back to three-way merge.
Patch failed at 0002 XXXXXXX
The copy of the patch that failed is found in:
   /xxxxxx/.git/rebase-apply/patch
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".

How to resolve this issue, if possible?

Any other Way to resolve this issue?


Solution

  • I would avoid the 'am' approach -- if you truly have common history you should take advantage of rebase, merge, or cherry.

    Assume you have a branch at E called 'master'

    Add the second repository as a remote to the first repository. Lets say I named the new remote 'sandbox'.

    fetch --all

    now create a new local branch at sandbox/C4 .. lets say that branch is named 'otherwork'. Check out that branch and rebase on top of 'master'. If you have trouble working through any resolves, you could check out master and cherrypick the changes one at a time from the 'otherwork' branch.