你好,我已经成功重写了历史记录,并使用
git filter-branch -f --prune-empty --tree-filter 'rm -rf <all unwanted dirs>'
获取了我想要提取的 5 个文件夹,并保留了所有 git 历史记录。
唯一剩下的问题是子模块,我仍然有提交
Subproject commit <hash>
并且我想从我的 git 历史记录中完全删除所有这些子模块提交,我该如何实现这一点?
我已经做到了
git filter-branch -f --prune-empty --tree-filter '
git submodule deinit -f .
git rm -rf lib && rm -rf lib
find . -name .gitmodules -delete' HEAD
假设我的所有子模块都位于
lib
目录
其他解决方案对我不起作用。按照关于如何删除子模块的投票最高的答案,我使用了以下命令,该命令更灵活,因为您可以在多个文件夹中指定子模块:
git filter-branch -f --prune-empty --tree-filter '
for SUBMODULE_DIR in submodule-dir-1 lib/submodule-dir-2 lib/submodule-dir-3
do
if [ -d $SUBMODULE_DIR ]; then
git submodule deinit -f $SUBMODULE_DIR
git rm -rf .git/modules/$SUBMODULE_DIR
git rm -f $SUBMODULE_DIR
fi
done
git rm -f --ignore-unmatch .gitmodules' HEAD
实际上,我们正在检查每次提交的子模块文件夹列表。如果子模块文件夹存在,我们完全删除该子模块。从技术上讲,仅此一项就足够了,我们还想删除
.gitmodules
文件。如果您只想删除特定的子模块,该行可能需要一些额外的工作。
我已经为此目的实现了脚本:
git_filter_branch_remove_paths.sh 删除过滤器分支路径.sh
使用示例:
git_filter_branch_remove_paths.sh -mip // // -- master
../../../remove-filter-branch-paths.sh -mip // // -- master