git: 将现有存储库拆分为子模块

git: Split existing repository into submodules

我只找到了有关如何使用 git 子树拆分存储库的答案。但是,我明确想要子模块。

这是一个 Java Maven 项目。目前,一切都在一个 maven 项目和一个存储库中。我的目标是实现这样的目标:

根存储库应包含主要 pom.xml、系统文档等 然后应该有几个子模块,一个用于实用程序库,一个用于主应用程序,等等。子模块是它们自己的 maven 项目,从根存储库中的主 maven 项目引用。根存储库将不包含任何源代码。

我可以从当前 HEAD 创建所有新内容,但对我来说重要的是尽可能完整地保留提交历史记录。

I only found answers on how to use git subtrees to split up a repository. However, I explicitly want submodules.

这正是您需要做的。使用 git subtree split <path> -b <branch> 将 "main" 拆分为分支,然后为每个子模块添加远程并将分支推送到远程。

# split the "main repo"
git subtree split -P path -b <branch1>

# For each branch that you extract

# add remote for branch 1
git remote add submodule1 <url>

# push the submodule
git push submodule1 <branch>

设置好所有子模块后,将它们添加到 "main" 存储库

# add the submodules 
git submodule add <url>

# and once all your submodules are added commit the .gitmodules file
# split the "main repo"
git subtree split -P path -b <branch>

# Create your repository, and get git url

# add remote for branch
git remote add submodule <url>

# push the submodule
git push -u submodule <branch>:master

# remove path
git rm -r path

# Stage and commit changes
git add -A
git commit -m 'Remove <path> for submodule replacement'

# add the submodule 
git submodule add <url> <path>

# and once your submodule is added commit the .gitmodules file