更新时间:2023-01-19 13:01:07
git subtree
我帮不上忙,但是filter-branch
可以帮上忙.
I can't help you with git subtree
, but with filter-branch
it's possible.
首先,您需要创建一个公共存储库,其中将包含源分支和目标分支.这可以通过在来源"旁边添加新的远程"并从新的远程获取来完成.
First you need to create a common repository that will contain both source and destination branches. This can be done by adding a new "remote" beside "origin" and fetching from the new remote.
在源分支上使用filter-branch
到rm -rf
除dir-to-move
以外的所有目录.之后,您将拥有一个提交历史记录,该记录可以被完全地重新建立基础或合并到目标分支中.我认为最简单的方法是从源分支中cherry-pick
所有非空提交.这些提交的列表可以通过运行git rev-list --reverse source-branch -- dir-to-move
Use filter-branch
on the source branch to rm -rf
all directories except dir-to-move
. After that you'll have a commit history that can be cleanly rebased or merged into the destination branch. I think the easiest way is to cherry-pick
all non-empty commits from the source branch. The list of these commits can be obtained by running git rev-list --reverse source-branch -- dir-to-move
当然,如果dir-to-move
的历史记录是非线性的(已经包含合并提交),那么它不会被Cherry-pick保留,因此可以使用git merge
代替.
Of course, if the history of dir-to-move
is non-linear (already contains merge commits), then it won't be preserved by cherry-pick, so git merge
can be used instead.
创建公共仓库的示例:
cd repo-2
git remote add source ../repo-1
git fetch source
示例过滤器分支
cd repo-2
git checkout -b source-master source/master
CMD="rm -rf dir1 dir2 dir3 dir5"
git filter-branch --tree-filter "$CMD"
示例性地摘录到目标主机
Example cherry-pick into destination master
cd repo-2
git checkout master
git cherry-pick `git rev-list --reverse source-master -- dir-to-move`