外部依存関係を管理するのではなく、単純に 2 つのリポジトリをくっつけて、最初からそうだったように見せようとしている場合、答えははるかに簡単であることがわかります。古いリポジトリにリモートを追加し、それらを新しいマスターにマージし、ファイルとフォルダーをサブディレクトリに移動し、移動をコミットし、追加のすべてのリポジトリに対して繰り返すだけです。サブモジュール、サブツリーのマージ、および派手なリベースは、わずかに異なる問題を解決することを目的としており、私がやろうとしていたことには適していません。
2 つのリポジトリを結合する Powershell スクリプトの例を次に示します。
# Assume the current directory is where we want the new repository to be created
# Create the new repository
git init
# Before we do a merge, we have to have an initial commit, so we'll make a dummy commit
git commit --allow-empty -m "Initial dummy commit"
# Add a remote for and fetch the old repo
# (the '--fetch' (or '-f') option will make git immediately fetch commits to the local repo after adding the remote)
git remote add --fetch old_a <OldA repo URL>
# Merge the files from old_a/master into new/master
git merge old_a/master --allow-unrelated-histories
# Move the old_a repo files and folders into a subdirectory so they don't collide with the other repo coming later
mkdir old_a
dir -exclude old_a | %{git mv $_.Name old_a}
# Commit the move
git commit -m "Move old_a files into subdir"
# Do the same thing for old_b
git remote add -f old_b <OldB repo URL>
git merge old_b/master --allow-unrelated-histories
mkdir old_b
dir –exclude old_a,old_b | %{git mv $_.Name old_b}
git commit -m "Move old_b files into subdir"
明らかに、代わりに old_b を old_a (新しい結合リポジトリになる) にマージすることもできます。それを行う場合は、スクリプトを適切に変更します。
進行中のフィーチャー ブランチも引き継ぎたい場合は、次のようにします。
# Bring over a feature branch from one of the old repos
git checkout -b feature-in-progress
git merge -s recursive -Xsubtree=old_a old_a/feature-in-progress
これは、プロセスの唯一の非自明な部分です。これは、サブツリーのマージではなく、ターゲットの名前を変更したことを Git に伝え、Git がすべてを正しく並べるのに役立つ、通常の再帰的マージの引数です。
もう少し詳しい説明をここに書きました。