9

I'm trying to migrate a few Git repositories into one monorepo.

I have two project repositories, let's call them project1 and project2. In my monorepo, I want to have a projects directory with two subdirectories, project1 and project2. Each subdirectory should contain the files from the corresponding project, with the Git history maintained.

Is this even possible with standard Git commands?

Note: I have looked at Lerna - lerna import does exactly what I need, but unfortunately it only works with JS projects, and one of my projects is a Ruby project.

Marina Liu
  • 36,876
  • 5
  • 61
  • 74
Joe Attardi
  • 4,381
  • 3
  • 39
  • 41
  • Similar: [Moving Git repository content to another repository preserving history](https://stackoverflow.com/questions/17371150/moving-git-repository-content-to-another-repository-preserving-history). – Flux Nov 03 '18 at 02:42
  • 2
    Consider using [tomono](https://github.com/unravelin/tomono), which automates the migration of many repos to one – ecoe May 01 '19 at 00:46
  • Does this answer your question? [How do you merge two Git repositories?](https://stackoverflow.com/questions/1425892/how-do-you-merge-two-git-repositories) – Inigo Jan 29 '23 at 02:26

1 Answers1

6

Yes, you can use git commands to achieve this.

Migrate two repos into a monorepo into subfolder project1 and project2, you need to move files into project1/project2 folder of a repo, commit changes and then combine them together. Detailed steps as below:

###1. Move files into project1 and project2 folders separately In the first repo (such as repo1), move files into project1 folder as below:

# In local repo1 
mkdir project1
mv * project1
git add .
git commit -m 'move files into project1 folder'
git push

In the second repo (such as repo2), move files into project2 folder as below:

# In local repo2
mkdir project2
mv * project2
git add .
git commit -m 'move files into project2 folder'
git push

###2. migrate the two repos into a monorepo

In any of a local repo (such as in local repo1), execute the below commands:

# In local repo1
git remote add repo2 <URL for repo2> -f
git pull repo2 master --allow-unrelated-histories
Harsh Patel
  • 6,334
  • 10
  • 40
  • 73
Marina Liu
  • 36,876
  • 5
  • 61
  • 74
  • 3
    How can I view all the previous commits for `project1`? If I do `git log -- project1` in the monorepo, there's only going to be one commit shown (i.e. `'move files into project1 folder'`). Any tips? – Flux Nov 20 '18 at 16:52