Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Migrate Git Projects to Be One Project with Subprojects

I have a number of projects that are in separate Git repositories right now. They are, likewise, in separate eclipse projects (because I was unable to use parent projects using the Maven plugin for a long time due to bugs in the m2 plugin). Now that works.

So I combined the projects in Maven, making a base pom project, adding that as the parent to the others. Then I nested the subprojects.

When I went to commit the base project as a git project, it had decided that the subdirectories were submodules, even though there was no .gitmodules file in the root directory.

Looks like the only way to get this done would be to lose all the history in the projects that are being combined.

Just to be super clear, current have:

Project A (repo A)
Project B (repo B)
Project C (repo C)

what I want is:

New Base Project D (repo D)
   /Project A
   /Project B
   /Project C

I would rather not lose any history but if I have to, I guess I could attic the prior versions. I don't think I want submodules, as those seem to be geared towards including remote repos that are not under your control.

Turned the solution into a bash script. It assumes that the subs you want to add are in subdirectories on the same level with the parent. Here it is:

#! /bin/bash
git remote add -f $1 ../$1
git merge -s ours --no-commit $1/master
git read-tree --prefix=$1 -u $1/master
git commit -m "Added project $1"

Git is amazing..

like image 619
Rob Avatar asked May 25 '11 00:05

Rob


1 Answers

You can do something called a "subtree merge" to combine multiple repositories into one. Let's suppose want to merge ~/projects/alpha and ~/projects/beta into a new project, ~/projects/multi:

$ cd ~/projects/multi
$ git init

Merge in the "alpha" project...

$ git remote add -f alpha ~/projects/alpha
$ git merge -s ours --no-commit --allow-unrelated-histories alpha/master
$ git read-tree --prefix=alpha -u alpha/master
$ git commit -m "Added project alpha"

Merge in the "beta" project the same way. If the change is permanent, then you can delete the alpha remote in your new project. If there is still development in the alpha repo, then you can not only keep history, but pull in new changes too.

All history will still be there, and the project will be in a subdirectory. I use this on my home computer for the "dead projects repository" -- a massive git repository containing everything I've ever worked on that is either abandoned or not large enough for its own repo.

like image 199
Dietrich Epp Avatar answered Nov 15 '22 17:11

Dietrich Epp