将一个带有所有分支和历史记录的git仓库中的一个子文件夹拆分出来

3
我想把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 

上一个命令似乎没有推送所有分支,我错过了什么吗?

你所提到的问题其实非常简短。我查看了 https://dev59.com/cXRC5IYBdhLWcg3wROpQ#17864475 并在那里找到了大部分信息。但是这两个都不完全符合我的问题。 - sha
这个回答解决了你的问题吗?git如何通过子文件夹拆分仓库并保留所有旧分支? - SpeziFish
1个回答

12

在这里找到了缺失的信息将子目录分离(移动)到单独的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

使用 filter-branch 命令来裁剪所需子文件夹中的所有内容(这将创建一个新的仓库,仅包含 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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接