Search code examples
gitversion-controlmonorepo

How to migrate multiple repositories into a mono-repo while maintaining commit history?


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.


Solution

  • 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