How to git merge unrelated history pre Git 2.9 (--allow-unrelated-histories isn't available)

Viewed 151

I know there's the option --allow-unrelated-histories which is available since Git 2.9 to merge unrelated histories. However, for some legacy corporate reasons, I'm stuck with an older version of Git 1.8.

Can anyone help showing how to do this without --allow-unrelated-histories?

1 Answers

If the two branches have points in their history that should naturally be stitched together (e.g : commits with the same content), you can use git replace (I don't know if it already existed in 1.8, though ?) or grafts to stitch the histories together :

# say <commit-a> on branch 'other' should be treated as
# <commit-b> on branch 'mine' :
git replace <commit-a> <commit-b>

# if 'git replace' does not exist :
# find the *descendant* of commit-a (say : child-a) and run :
echo "<child-a> <commit-b>" >> .git/info/grafts

# <child-a> is now treated as if <commit-b> was one of his parents

You should then be able to run git merge.

Remove the replace (git replace -d <commit-a>) or the graft (remove line from .git/info/grafts, or the whole file if you don't have any other grafts).


Another option is to dump the complete content of branch other as a patch (for example : diff it against a fake commit which has an empty content), and use git apply :

git diff <commit with empty content> other > other.patch
git apply other.patch
Related