我想将我的 git 存储库中的文件夹拆分为新文件夹。但我想保留与此文件夹相关的所有打开/关闭的分支和历史记录。
Source Repo
-- AAA
-- BBB
---- DDD
---- EEE
-- CCC
New Repo 1
-- AAA
-- CCC
New Repo 2 (BBB subfolder)
-- DDD
-- EEE
我按照此处描述的步骤 https://help.github.com/articles/splitting-a-subfolder-out-into-a-new-repository/- 但这仅适用于单个分支。最后 - 我得到了包含所有 (?) 提交的新存储库,但没有分支信息。
我尝试使用 --all
参数对所有分支执行此操作,但我不清楚如何将所有重写的分支推送到空的远程存储库。
这是我目前拥有的:
$ git clone https://github.com/USERNAME/REPOSITORY-NAME
$ cd REPOSITORY-NAME
$ git filter-branch --prune-empty --subdirectory-filter FOLDER-NAME -- --all
在这一点上,如果我查看我的本地存储库,我似乎拥有远程来源中的所有历史记录和所有分支。
$ git remote -v
origin https://github.com/USERNAME/REPOSITORY-NAME.git (fetch)
origin https://github.com/USERNAME/REPOSITORY-NAME.git (push)
$ git remote set-url origin https://github.com/USERNAME/NEW-REPOSITORY-NAME.git
$ git remote -v
origin https://github.com/USERNAME/NEW-REPOSITORY-NAME.git (fetch)
origin https://github.com/USERNAME/NEW-REPOSITORY-NAME.git (push)
$ git push -u origin --all
最后一个命令似乎没有推送所有分支。我错过了什么?
在这里发现缺少的信息 将子目录分离(移动)到单独的 Git 存储库中
完成的脚本如下所示。对两个新存储库使用此过程,其中一行不同
克隆源存储库:
$ git clone https://github.com/USERNAME/REPOSITORY-NAME
$ cd REPOSITORY-NAME
在本地重新创建所有远程分支
$ for i in $(git branch -r | sed "s/.*origin///"); do git branch -t $i origin/$i; done
使用过滤器分支修剪所需子文件夹的所有内容(这将仅使用 FOLDER-NAME 创建新的存储库
$ git filter-branch --prune-empty --subdirectory-filter FOLDER-NAME -- --all
使用filter-branch删除FOLDER-NAME(这将创建一个新的存储库,其中包含除FOLDER-NAME之外的所有内容)
$ git filter-branch --prune-empty --tree-filter 'rm -rf FOLDER-NAME' -- --all
将远程 URL 更新为新的空存储库:
$ git remote -v
origin https://github.com/USERNAME/REPOSITORY-NAME.git (fetch)
origin https://github.com/USERNAME/REPOSITORY-NAME.git (push)
$ git remote set-url origin https://github.com/USERNAME/NEW-REPOSITORY-NAME.git
$ git remote -v
origin https://github.com/USERNAME/NEW-REPOSITORY-NAME.git (fetch)
origin https://github.com/USERNAME/NEW-REPOSITORY-NAME.git (push)
将所有内容整合在一起
$ git push -u origin --all