How to combine two repositories with same files with un-related history

Requirement:

  1. There are two repositories
  2. There are common file between these two repositories
  3. An older one, lets call it as parent
  4. The newest one, lets call it as child
  5. We want to apply all changes from child to parent
  6. There are no common ancestor between child and parent 
Problem Statement:
The simplest way would be to fork the child repository into parent repository and issue:
git merge child_repo/master

However, as there is no common ancestor, git will reject this merge.
Another option is to then add --allow-unrelated-histories option to merge.
git merge child_repo/master --allow-unrelated-histories

However, this results into merge-conflicts

Workaround:
Identify which all commits are to be taken from child to parent. e.g.
git rev-list child_repo/master

e.g. there are 3 commits in child_repo/master and except the first commit in child_repo/master every change is needed and to be applied on top of parent, then issue following command.
git rev-list child_repo/child --reverse --max-count=2 | git cherry-pick --stdin

This will reverse the commit order of child_repo/master and apply one-by-one to parent.

Visualization:

Before:
child: [c0]---[c1]---[c2]
parent:[p1]---[p2]---[p3]---[p4]


After:
child: [c0]---[c1]---[c2]
parent:[p1]---[p2]---[p3]---[p4]---[c1']--[c2']

Preparation thoughts:
The repositories can be locally forked, e.g. directories for parent and child
parent: c:\git\parent\.git
child: c:\git\child\.git

Now, open git-bash at parent and issue following command:
git remote add child_repo ..\child\.git

and get latest commits from child as:
git pull child_repo

Comments