Git octopus merge with unrelated repositories

I've managed to resolve the problem with octopus merge of unrelated branches. Unfortunately the following applies only to rather simple cases where no real merge conflicts exist.

  1. Do a merge as described in the OP.

    git merge --allow-unrelated-histories a/master b/master c/master d/master

    It will "fail" claiming about conflicts. But the merge has been performed and that fact recorded although there are no changes in the index.

  2. Add contents of branches to the index with the read-tree command:

    git read-tree a/master b/master c/master d/master

    This will not affect working tree, only the index will be updated. (It is possible that you want to add your current branch to the end. You need to add all of them in one command.)

    If branches are not totally independent then adjust their order keeping in mind that the latter will overwrite contents of former branches.

  3. Commit normally. (git commit -m "octopus-merged remote 'a', 'b', 'c', 'd'")
  4. Perform a hard reset (git reset --hard) to get a consistent working tree.

I suppose one can later edit and amend this commit but I personally haven't tried this.


Create an empty commit

git commit -m "Common commit" --allow-empty

look at its hash with log -1 and add it to the grafts file without any parents

echo HASH_OF_COMMON_COMMIT >> .git/info/grafts

then find all other roots with log --max-parents=0 and for each add their hash to the graft file with the above hash as parent

each HASH_OF_ROOT HASH_OF_COMMON_COMMIT >> .git/info/grafts

and now, merge!

The grafts file is a way to change the parents of a commit and once all your repos have a common commit your repo should accept the merge.

Tags:

Git

Git Merge