0

I am trying to create a monorepo from multiple existing repos. I have created a project with its own .git, README.md, ... and a /packages folder, where I want to place the merged repos as subfolders. But after I do this

cd packages
git remote add existing-1 git@...
git fetch --all
git merge existing-1/master --allow-unrelated-histories

I get merge conflicts (README.md etc), because git wants to merge the repo into root, not /packages. What can I do?

Phil
  • 7,065
  • 8
  • 49
  • 91

2 Answers2

2

The quickest way to do this is to copy all the files from the old repo into the new repo and make one very large commit.

If you insist on maintain histories, I can think of two ways:

  1. Start a clean repo and fetch the old repo like you are already doing. Then git mv the files into the new subfolder structure that you want. Rinse and repeat for each of the projects you want to add to the new repo. Then finally add the scafolding for your monorepo.

  2. In the old repo, git mv the files to the directory structure you want. Then merge into the new repo similar to what you are already doing.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Thanks, if I wanted to do it manually, I would do it that way. See my answer for a shortcut – Phil Apr 17 '23 at 06:11
1

Sorry, I can't close the question since it is answered already. I found git-subtree to be the best solution. I just need to do

git subtree add -P packages/existing-1 git@... master

where git@... is the remote URL. See https://stackoverflow.com/a/32684526/4125622

Phil
  • 7,065
  • 8
  • 49
  • 91