Created
March 28, 2019 20:56
-
-
Save benhubert/347f7d0d48bd696e3f8e8c53bee68cc4 to your computer and use it in GitHub Desktop.
Merge git repositories and keep their history
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Initialize the new repository in an empty directory | |
git init | |
# Before we can do a merge, we need some initial commit. Create a file that's | |
# not part of any of the repos you want to merge, for example | |
touch delete-me-afterwards.txt | |
git add . | |
git commit -m "initial commit" | |
# Add the remote of one of your repositories you want to merge | |
# -f tells git to automatically fetch it | |
git remote add -f repo_a <remote url for repo A> | |
# Merge the files from the remote repo into the new one | |
git merge --allow-unrelated-histories \ | |
-m "Merged repo_a from its repo to the new monorepo" \ | |
repo_a/master | |
# Move the files from repo_a to their new subdirectory | |
mkdir subdir_a | |
find . -maxdepth 1 \ | |
-not -path "." \ | |
-not -path "./.git" \ | |
-not -path "./delete-me-afterwards.txt" \ | |
-not -path "./subdir_a" \ | |
-exec git mv '{}' 'subdir_a/{}' \; | |
git commit -m "Moved files from repo_a to their new subdirectory" | |
# Remove the remote repository | |
git remote remove repo_a | |
# Repeat the above steps for any other repository you want to merge. Don't | |
# forget to exclude subdir_a from the next find command. | |
# Remove the dummy file | |
git rm ./delete-me-afterwards.txt | |
git commit -m "Removed temporary initial file" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment