git - 将带有子模块的子目录拆分为单独的 git 存储库

标签 git git-filter-branch

作为问题的子集 detach-subdirectory之前已经做过,考虑到即使有很多关于拆分和 merge git 存储库的过程的问题,我也找不到涉及子模块存在时拆分主题的问题。

所以在下面的场景中:

.git/
.gitmodules
folder/
    data/
    content/
        other_data/
        submoduleA/
        submoduleB/

我想获得两个具有以下结构的存储库:

.git/
data/

.git/
.gitmodules
content/
    other_data/
    submoduleA/
    submoduleB/

第一种情况不是问题,可以通过detach-subdirectory中描述的方法轻松解决。 .

第二个没那么多。子模块的存在以及 .gitmodules 包含 folder/content/submoduleAfolder/content/submoduleB 的完整路径这一事实导致部分历史记录自 . gitmodules 指的是一个不存在的目录结构(一旦使用了 filter-branch)。

所以我想知道是否有一种方法可以在不导致历史不一致的情况下做到这一点。

最佳答案

我遇到了与 Unode 完全相同的问题,并通过以下过程设法解决了它:

git clone git@github.com:kdeldycke/kev-code.git
cd kev-code
git filter-branch --tree-filter "test -f ./.gitmodules && mv ./.gitmodules ./cool-cavemen/gitmodules || echo 'No .gitmodules file found'" -- --all
git filter-branch --force --prune-empty --subdirectory-filter cool-cavemen --tag-name-filter cat -- --all init..HEAD
git filter-branch --force --tree-filter "test -f ./gitmodules && mv ./gitmodules ./.gitmodules || echo 'No gitmodules file found'" -- --all
git filter-branch --force --tree-filter "test -f ./.gitmodules && sed -i 's/cool-cavemen\///g' ./.gitmodules || echo 'No .gitmodules file found'" -- --all
git remote rm origin
rm -rf .git/refs/original/
git reflog expire --all
git gc --aggressive --prune
git remote add origin git@github.com:kdeldycke/cool-cavemen.git
git push -u origin master --force --tags

如您所见,技巧是临时重命名 .gitmodules 文件并使用 sed 重写其内容。您可以获得所有详细信息和 context of this procedure on my blog .

关于git - 将带有子模块的子目录拆分为单独的 git 存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3411364/

相关文章:

git - "git unable to read current working directory no error"- 豪华 git (windows 10)

git - 如何避免 git 中的分离头状态?

Git:如何在特定提交之前删除历史记录

git - 在 Visual Studio 中更改 Git 作者信息

Git 克隆多个特定分支

git - 在 Visual Studio 代码中设置 github 个人访问 token

远程服务器上需要安装 git,只能通过 ssh 访问

git 将所有内容向上移动一级,适用于所有分支

git - 如何在不修改 git 历史记录的情况下对源代码运行代码格式化程序?

git - 为什么 no-op filter-branch 会产生分歧,我该如何解决?