git clone initial final
cd final
git filter-branch --tree-filter 'rm -rf <list of folders separated by space>' HEAD
上述命令将删除列表中提到的任何文件夹,但您的原始提交历史记录将保留。
git filter-branch -f --prune-empty
然后可用于删除可能显示在已删除文件夹列表中的任何空提交。
或者,如果您有一个历史悠久的大型存储库,这可能需要一段时间。在这种情况下,你可以采取稍微不同的方法 -
首先为要迁移的每个文件夹创建单独的存储库。这可以使用 filter-branch 命令来完成,如下所示
git filter-branch –prune-empty –subdirectory-filter A/ master
git filter-branch –prune-empty –subdirectory-filter B/ master
每个文件夹都包含各自文件夹的内容及其所有历史记录。然后,您可以将它们作为临时存储库推送到远程。 (如果您需要重新执行该过程,这些也将充当检查点)
接下来,创建新的存储库
git clone <remote path> NewRepo
cd NewRepo
# add a readme file
touch Readme.md
git add .
git commit -am "Adding ReadMe file"
然后,您可以将各个文件夹(A 和 B 存储库)合并到 NewRepo 中
# Merge Repo A
git remote add -f A <remote path for A>
git merge -s ours --no-commit A/master
git read-tree --prefix=A/ -u A/master
git commit -m "Merge A into NewRepo"
git remote remove A
# Merge Repo B
git remote add -f B <remote path for B>
git merge -s ours --no-commit B/master
git read-tree --prefix=B/ -u B/master
git commit -m "Merge B into NewRepo"
git remote remove B
接下来您可以确认您的来源已设置为指向 NewRepo 并推送代码
git remote add origin <remote path for NewRepo>
git push origin master
NewRepo 现在应该包含文件夹及其所有历史记录。