使用简单 git 命令的手动步骤
计划是将各个目录拆分为自己的存储库,然后将它们合并在一起。以下手动步骤没有使用极客使用的脚本,而是使用易于理解的命令,并且可以帮助将额外的 N 个子文件夹合并到另一个存储库中。
除法
假设您的原始仓库是:original_repo
1 - 拆分应用:
git clone original_repo apps-repo
cd apps-repo
git filter-branch --prune-empty --subdirectory-filter apps master
2 - 拆分库
git clone original_repo libs-repo
cd libs-repo
git filter-branch --prune-empty --subdirectory-filter libs master
如果您有 2 个以上的文件夹,请继续。现在您将拥有两个新的临时 git 存储库。
通过合并应用程序和库来征服
3 - 准备全新的 repo:
mkdir my-desired-repo
cd my-desired-repo
git init
并且您将需要至少进行一次提交。如果应该跳过以下三行,您的第一个 repo 将立即显示在 repo 的根目录下:
touch a_file_and_make_a_commit # see user's feedback
git add a_file_and_make_a_commit
git commit -am "at least one commit is needed for it to work"
提交临时文件后,后面部分中的merge 命令将按预期停止。
根据用户的反馈,您可以选择添加.gitignore或README.md等随机文件,而不是添加a_file_and_make_a_commit这样的随机文件。
4 - 先合并应用程序仓库:
git remote add apps-repo ../apps-repo
git fetch apps-repo
git merge -s ours --no-commit apps-repo/master # see below note.
git read-tree --prefix=apps -u apps-repo/master
git commit -m "import apps"
现在您应该会在新存储库中看到 apps 目录。 git log 应该显示所有相关的历史提交消息。
注意:正如 Chris 在下面的 cmets 中所述,对于较新版本 (>=2.9) 的 git,您需要指定 --allow-unrelated-histories 和 git merge
5 - 以同样的方式合并 libs repo:
git remote add libs-repo ../libs-repo
git fetch libs-repo
git merge -s ours --no-commit libs-repo/master # see above note.
git read-tree --prefix=libs -u libs-repo/master
git commit -m "import libs"
如果要合并的存储库超过 2 个,请继续。
参考:Merge a subdirectory of another repository with git