具有相关历史的部分 git 克隆

Partial git clone with relevant history

假设我已经编写了无数个 puppet 模块,并且我在一个 git 存储库中拥有它们的全部历史,但现在我真的希望我为每个模块制作一个存储库。划分文件结构很容易,因为每个文件都完全包含在自己的目录中,并且组织方式类似于 GIT_ROOT/modules/NAME

有没有一种方法可以将这个 repo 分开而不丢失每个模块的历史记录?理想情况下,每个 repo 只具有与其代表的模块相关的历史记录。我尝试克隆整个东西和 git rm -rf 所有不相关但保留不相关历史的东西。

我计划将它们与 git 个子模块粘合在一起,FWIW。

我一直在玩这个,看起来最简单的拆分方法是使用 git subtree split:

split

Extract a new, synthetic project history from the history of the subtree. The new history includes only the commits (including merges) that affected , and each of those commits now has the contents of at the root of the project instead of in a subdirectory. Thus, the newly created history is suitable for export as a separate git repository.

因此,例如,如果我从 openstack-puppet-modules 存储库开始,其中包含一堆单独的木偶模块,我可以先将它们分成单独的分支,就像这样(我在这里只使用八个模块保持简短):

for x in apache aviator ceilometer certmonger cinder common \
    concat firewall; do
  git subtree split -P $x -b split-$x
done

完成后 运行,我有:

$ git branch | grep split-
split-apache
split-aviator
split-ceilometer
split-certmonger
split-cinder
split-common
split-concat
split-firewall
split-pacemaker

这些分支中的每一个都只包含特定的历史 目录。如果我想将它们转换成单独的存储库,我 可以这样做:

for x in apache aviator ceilometer certmonger cinder common \
    concat firewall; do
  git init --bare ../repos/repo-$x
  git push ../repos/repo-$x split-$x:master
done

现在我有一个存储库集合:

$ ls ../repos/
repo-apache  repo-aviator  repo-ceilometer  repo-certmonger
repo-cinder  repo-common  repo-concat  repo-firewall  work-cinder

而且我认为我们已经实现了您的目标。