首页 > 解决方案 > 当项目包含其他子模块时 git filter-branch 不起作用

问题描述

假设我有以下项目结构:

.git

--APP
-----F

--LIBS
------A
-------.git
------B
-------.git
------C
-------.git
------D
-------.git

F是APP文件夹下的子文件夹。 A, B, C并且D是位于 LIBS 文件夹下的四个子模块。

我想要做的是将F文件夹提取到不同的存储库(目前它只是一个简单的子文件夹)。

到目前为止,我已经完成了以下工作:

for remote in `git branch -r | grep -v /HEAD`; do git checkout --track $remote; done

之后,我跑了:

git filter-branch --prune-empty --tag-name-filter cat --subdirectory-filter APP/F/ -- --all

结果是:

.git

--F

--LIBS
------A
-------.git
------B
-------.git
------C
-------.git
------D
-------.git

似乎层次结构中的子模块仍然改写了历史。

但是,我想要的结果是只保留 F 目录,没有其他子模块..:

--F
---.git

标签: gitgit-filter-branch

解决方案


filter-branch不是使用子模块的正确工具。您想要的是删除相应的子模块(使用 rm),然后使用 filter-branch 移​​动层次结构根,如下所示:

rm -r LIBS && git add --all && git commit -m "Dropped submodules"
git filter-branch --prune-empty --tag-name-filter cat --subdirectory-filter APP/F/

推荐阅读