git - 将子目录移动到分支

git - Move Subdirectories into Branches

我有一个 git 存储库,其根目录中有几个子目录,例如

repo<master>/a/files
repo<master>/b/files
...

我想将每个子目录移出 master 分支并移到它自己的分支中,并在此过程中将文件向上移动一个目录,这样我就剩下这样的东西了。

repo<branch_a>/files
repo<branch_b>/files
...

我有一些想法,例如:

  1. 正在从 master 中删除所有项目,创建一个新分支,复制该分支所需的文件,提交。这样做的问题是我会丢失文件的提交历史记录。
  2. 分支、删除我不需要的目录、将文件上移一级、提交。这将保留历史记录,但我要在每个分支中进行大量相同的删除。

无论如何我可以更有效地做到这一点,即减少删除但仍保留修订历史记录?

这个问题与 Detach (move) subdirectory into separate Git repository 类似,但我不想将每个子目录移动到单独的存储库中,而是将其移动到自己的分支中。

这里有一个比较简洁的方法:

  • 删除 master 上的所有子目录,每个都在单独的提交中
  • 根据master
  • 创建您想要的每个分支
  • 在每个分支上,恢复删除分支想要的目录的提交
  • 目录上移

伪 bash:

$ for dir in one two three; do git rm -r $dir; git commit -m "Remove $dir"; done
$ git log # For commit SHAs
$ git checkout -b one
$ git revert $sha_that_removed_one
$ git mv one/* .
$ git commit -m "Move one files to the top level"

这似乎是个好地方git submodules