Search code examples
gitgit-merge

Git octopus merge with unrelated repositories


I have a bunch of git repositories, each containing one file. I'd like to merge them all together, preferably in one step. What I'm aiming for is this graph:

*----¬ mergedrepo/master
| \ \ \
| | | * repoA/master
| | * repoB/master
| | |
| | * repoB/...
| * repoC/master
* repoD/master
|
* repoD/...

I tried a git merge, but it appears the octopus strategy doesn't work for disjoint trees

$ git merge a/master b/master c/master d/master
Unable to find common commit with a/master
Automatic merge failed; fix conflicts and then commit the result.

I was also told that git merge --squash would help, but that gave the same error.

This generates the right graph, but loses all the files:

$ git merge -s ours a/master b/master c/master d/master

How do I go about doing this?


Solution

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