如何将 Git 项目迁移为一个包含子项目的项目

发布于 2024-11-09 16:22:42 字数 825 浏览 3 评论 0原文

我现在有许多项目位于单独的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴梦一场 2024-11-16 16:22:42

您可以执行称为“子树合并”的操作,将多个存储库合并为一个。假设想要将 ~/projects/alpha~/projects/beta 合并到一个新项目中,~/projects/multi

$ cd ~/projects/multi
$ git init

合并“alpha”项目...

$ 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"

以同样的方式合并到“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:

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

雅心素梦 2024-11-16 16:22:42

我没有足够的声誉来评论你的历史问题,所以我必须写一个答案。

使用 --follow 记录日志

--follow
       Continue listing the history of a file beyond renames (works only
       for a single file).

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).
青丝拂面 2024-11-16 16:22:42

我要做的就是寻找子模块。但是,如果您确实想合并存储库,请执行以下操作:

  • 创建新存储库
  • 进行初始提交,
  • 从那里为每个要合并的项目创建分支,
  • 以合并每个项目:
    • 切换到其临时分支
    • 合并原始存储库
    • 将其移动到项目目录(使用 git mv)
    • 提交
  • 合并(到 master)并删除每个临时项目分支

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文