8

I have a one git project with a file structure like this:

    Project_A/files...

I have another git project with a file structure like this:

    Project_B/
        Project_A/files...
        files...

Now I want to merge Project A into Project B and continue using Project B as the sole repository.

I tried using the subtree merge, but I got an error saying "Entry 'XXX' overlaps 'XXX'"

Is there a way to merge Project A into Project B and retain all of the commit histories?

Thanks in advance!

dirtytofu
  • 245
  • 4
  • 9

2 Answers2

11

You could do something like this:

In Project_A, make a new Project_A subdirectory and git mv everything into it, so Project_A now looks like

Project_A/
    Project_A/files...

Then, in Project_B:

git remote add project_A Project_A
git fetch project_A
git branch project_A project_A/master
git checkout -b merge_trial master
git merge project_A

... and fix as necessary on merge_trial (or lather, rinse, repeat until you get what you want regarding conflicts/overlaps).

I've actually done something exactly like this as part of an svn->git migration.

ebneter
  • 20,795
  • 9
  • 30
  • 30
  • Instead of git-mv is there a way to also move my files so that all of my previous commits in Project_A also show up as the new modified structure? I think it has to do something with git-filter-branch, but I haven't tested that command out yet. – dirtytofu Feb 10 '10 at 08:23
  • Yes, you could use git filter-branch to accomplish the same thing. git mv might be easier in this case, though. In any event, when you fetch project_A into Project_B, all the history will come with it. – ebneter Feb 10 '10 at 08:33
  • 1
    For the record, this works instead of `git mv`: `git filter-branch --tree-filter 'FILES=$(ls 1 | grep -v Project_A); mkdir -p Project_A; mv $FILES Project_A' HEAD`. Do this on `project_A` to move everything down one directory. – Daniel C. Sobral Aug 09 '12 at 19:15
  • That said, the last example on filter-branch man page has what seems to be safer way of doing it. – Daniel C. Sobral Aug 09 '12 at 21:29
  • Thanks, Daniel, for providing the filter-branch example. – ebneter Aug 09 '12 at 21:47
1

If projectB already contains projectA as a submodule, you should:


If projectA was not a submodule of projectB, I would recommend fetching projectA into projectB repo, and then use the graft technique to link the two commit lines together, while not dealing with all the merge conflicts a classical merge would have involved.

See question Git question: possible to merge two different by equal repositories?

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 2
    You mean "subtree merge?" Although I rather like the submerge tree. – ebneter Feb 10 '10 at 07:02
  • @ebneter: fixed. I should not answer questions at 6AM ;) – VonC Feb 10 '10 at 07:06
  • Project_A is not a submodule of Project_B currently. Two separate development repositories were started by two different teams and now I'm trying to merge my teams repository into theirs, but the different file structure of the projects seem to be causing me some issues. – dirtytofu Feb 10 '10 at 08:25