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 g开发者_如何学编程et 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..
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.
I don't have enough reputation to comment on your history question, so I have to write an answer.
Use --follow with log
--follow
Continue listing the history of a file beyond renames (works only
for a single file).
What I would do is go for submodules. But if you really want to combine the repos, do the following:
- create the new repository
- make an initial commit
- make branches from there to every project what you want to merge
- for every project that you want to merge:
- switch to its temporary branch
- merge the original repository
- move it to its project directory (with git mv)
- commit
- merge (to master) and delete every temporary project branches
精彩评论