为了将main
发生在两件事中的所有相关变化带入external
需要发生的事情:
- 隔离原件
main
- 将
main
发生在external
首先,隔离原始main
:
似乎您可能main
在自己的存储库中。
external
如果不是,只要将原始main
提交设置为创建external/main
子目录的提交的父级,就可以从存储库中检索它。一个例子是Git Book Subtree Merging 页面中概述的过程
提交可以找到引入的子树,如this answer中所述。
然后只需抓住作为基础的整个提交集main
并从中创建一个存储库。
其次,将main
发生在的所有提交纳入其中external
:
您已经隔离了包含exteral/main
子文件夹中更改的提交,但正如您所说,它不包括原始main
提交。
git filter-branch --tag-name-filter cat --prune-empty --subdirectory-filter main -- --all
这是因为filter-branch
只会检查特定子目录位置中的文件,而不知道如何处理更复杂的操作,例如read-tree
创建子目录的操作。
操作后filter-branch
,您将留下一组提交,其中包含在main/external
. 让我们假设可以在filteredMain
分支中访问这组提交。
由于内容已从子目录移动到根目录,因此文件的位置现在再次与main
存储库中的内容相同。这将允许连接两棵树。由于这两个树main
的主分支并filterBranch
没有共同的历史记录,因此可以通过rebase
重放提交的更改来加入它们。
# in the main repository
# bring the external repository and get the branch
git remote add external /path/to/external
git fetch external filteredMain
git checkout filteredMain
# We need the first commit of this tree for the rebase command
firstCommit=$(git rev-list --max-parents=0 HEAD)
# run the rebase
git rebase --onto master $firstCommit filteredMain
在此之后,分支应该包含在存储库中的原始分支之上重放的filteredMain
所有更改。external/main
master
main