Friday, November 8, 2019

Merge multiple git repositories into one repository

Sometimes you may have multiple code repositories which you always end up checking out together, and deploying together.  Or maybe it seemed like a good organization idea to separate your code based on functionality, but in practice, it has become cumbersome.

With a few commands, you can merge the multiple repositories into one repository, keeping their history.  You can also keep the separate repositories in their own sub directory, thus maintaining the organization of your code, but utilize one repository to facility development, branches, and deployments.

Example list of current separate repositories:
    ls 
    /local_git/old_project_1
    /local_git/old_project_2
Update your local repositories with the latest code
Pull and Commit/Push any changes

Create a new repository for the combined project and push
    cd /local_git
    mkdir new_combined_project
    touch README.md
    git init .
    git commit -m "add readme"
    git remote add origin remote https://github.com/your_repo_url
    git push

Add the first separate repository to the new combined repository
    cd /local_git/new_combined_project
    git remote add old_project_1 ../old_project_1

List repositories
    git remote -v    

    old_project_1  ../old_project_1.git (fetch)
    old_project_1  ../old_project_1.git (push)
    origin  https://github.com/your_repo_url.git (fetch)    origin  https://github.com/your_repo_url.git (push)
 
Note: If you get the wrong path to your local repository, you can remove repository entries
    git remote remove old_project_1

Fetch the branch/tags/master for the first separate repository
    git fetch old_project_1 --tags

Merge the files and histories for the first separate repository
    git merge --allow-unrelated-histories old_project_1/master
    list of files

You should have a list of files and directories from the first separate repository
    ls
    your files from old_project_1


Optionally create a sub directory to move the files into.
    cd /local_git/new_combined_project
    mkdir old_project_1
 
Move the files and folders into the new nested directory
    git mv !(old_project_1|old_project_2) old_project_1

Note: If you just mv or cut/paste the files into the new directory, git may not persist the history for those files.

Note: !() excludes the listed directory/file

Note: If you get an error about unknown bash command !, enable the glob extension
    shopt -s extglob

Your directories/files from the first separate repository should now be in
    /local_git/new_combined_project/old_project_1
and you should have the git history for old_project_1

Consolidated commands repeating for the second separate repository
    cd /local_git/new_combined_project
    ls
    git remote add old_project_2 ../old_project_2
    git remote -v
    git fetch old_project_2 --tags
    git merge --allow-unrelated-histories old_project_2/master
    mkdir old_project_2
    git mv !(old_project_1|old_project_2) old_project_2
    ls
    ls old_project_2

Your multiple separate repositories are now merged into one repository, with their history.  After verifying by checking out to a new directory, viewing it's history, you can remove the prior separate repositories.

Reference: https://stackoverflow.com/a/10548919


-End of Document-
Thanks for reading