如何将 Git 项目迁移为一个包含子项目的项目
我现在有许多项目位于单独的 Git 存储库中。同样,它们位于单独的 eclipse 项目中(因为由于 m2 插件中的错误,我很长一段时间无法使用 Maven 插件来使用父项目)。现在可以了。
因此,我将 Maven 中的项目组合起来,创建一个基础 pom 项目,并将其添加为其他项目的父项目。然后我嵌套了子项目。
当我将基础项目作为 git 项目提交时,它决定子目录是子模块,即使根目录中没有 .gitmodules 文件。
看起来完成此任务的唯一方法是丢失正在合并的项目中的所有历史记录。
非常清楚的是,当前的:
Project A (repo A)
Project B (repo B)
Project C (repo C)
我想要的是:
New Base Project D (repo D)
/Project A
/Project B
/Project C
我宁愿不丢失任何历史记录,但如果必须的话,我想我可以阁楼以前的版本。我不认为我想要子模块,因为这些子模块似乎旨在包括不受您控制的远程存储库。
将解决方案变成了 bash 脚本。它假定您要添加的子目录位于与父目录同一级别的子目录中。就是这样:
#! /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 太神奇了..
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..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以执行称为“子树合并”的操作,将多个存储库合并为一个。假设想要将
~/projects/alpha
和~/projects/beta
合并到一个新项目中,~/projects/multi
:合并“alpha”项目...
以同样的方式合并到“beta”项目中。如果更改是永久性的,那么您可以删除新项目中的 alpha 遥控器。如果 alpha 存储库仍有开发,那么您不仅可以保留历史记录,还可以引入新的更改。
所有历史记录仍然存在,并且该项目将位于子目录中。我在我的家用计算机上使用它作为“死项目存储库”——一个巨大的 git 存储库,其中包含我曾经处理过的所有内容,这些内容要么被放弃,要么不够大,无法容纳自己的存储库。
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
:Merge in the "alpha" project...
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.
我没有足够的声誉来评论你的历史问题,所以我必须写一个答案。
使用 --follow 记录日志
I don't have enough reputation to comment on your history question, so I have to write an answer.
Use --follow with log
我要做的就是寻找子模块。但是,如果您确实想合并存储库,请执行以下操作:
What I would do is go for submodules. But if you really want to combine the repos, do the following: