如何将Git项目迁移到一个具有子项目的项目 [英] How to Migrate Git Projects to Be One Project with Subprojects

查看:239
本文介绍了如何将Git项目迁移到一个具有子项目的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有许多项目在不同的Git仓库中。他们同样也在单独的eclipse项目中(因为m2插件中的错误导致我无法长时间使用Maven插件使用父项目)。现在可以工作了。

因此,我将Maven中的项目合并为一个基础项目,并将其作为其他项目的父项。然后我嵌套子项目。

当我将基础项目提交为git项目时,它已经决定子目录是子模块,即使没有.gitmodules文件在根目录中。



看起来,完成这项工作的唯一方法是丢失正在合并的项目中的所有历史记录。



$ p
$ b

 项目A(回购A)
项目B(回购B)
项目C(回购C)

我想要的是:

 新建基地项目D(回购D)
/项目A
/项目B
/项目C

我宁愿不失去任何历史记录,但如果必须的话,我想我可以在以前的版本。我不认为我需要子模块,因为那些似乎适用于包含不受您控制的远程回购。



将解决方案转换为bash脚本。它假设你想添加的子目录位于与父目录相同级别的子目录中。这里是:

 #! / bin / bash 
git remote add -f $ 1 ../$1
git merge -s我们的--no-commit $ 1 / master
git读取树--prefix = $ 1 -u $ 1 / master
git commit -m已添加项目$ 1

解决方案

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

在alpha项目中合并...

  $ git remote add -f alpha〜/ projects / alpha 
$ git merge -s our --no-commit alpha / master
$ git read-tree --prefix = alpha -u alpha / master
$ git commit -m新增项目alpha

以同样的方式合并测试版项目。如果更改是永久性的,那么您可以删除新项目中的alpha远程。如果在alpha回购中仍有发展,那么你不仅可以保留历史,而且还可以引入新的变化。



所有的历史记录仍然存在,项目将在一个子目录中。我在家用电脑上使用了这个死亡项目库 - 一个包含我曾经工作过的所有东西的大规模git仓库,要么被放弃,要么不够大,不足以用于它自己的repo。


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

解决方案

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

这篇关于如何将Git项目迁移到一个具有子项目的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆