Suppose you have a case where you have multiple repos and you must combine them into a single monorepo. It is straightforward to just simply import a repo by copy pasting the contents into a sub folder.

But what if you want to save the contents and the git history of the repo which is to be imported.

For this, we can use:

git remote add -f <remote-name> <remote-url>
git subtree add --prefix=<directory> <remote-name> <branch>

Replace <remote-name>, <remote-url>, <directory>, and <branch> with your desired values. For example:

  • <remote-name>: A name for the remote repository (e.g., my-remote).
  • <remote-url>: The URL of the remote repository (e.g., [email protected]:yourgroup/yourproject.git).
  • <directory>: The directory where you want to add the subtree (e.g., subtree-dir).
  • <branch>: The branch you want to add from the remote repository (e.g., main or master).

ref: Git Subtree basics · GitHub