Manual steps with simple git commands
The plan is to split individual directories into its own repos, then merge them together. The following manual steps did not employ geek-to-use scripts but easy-to-understand commands and could help merge extra N sub-folders into another single repository.
Divide
Let's assume your original repo is: original_repo
1 - Split apps:
git clone original_repo apps-repo
cd apps-repo
git filter-branch --prune-empty --subdirectory-filter apps master
2 - Split libs
git clone original_repo libs-repo
cd libs-repo
git filter-branch --prune-empty --subdirectory-filter libs master
Continue if you have more than 2 folders. Now you shall have two new and temporary git repository.
Conquer by Merging apps and libs
3 - Prepare the brand new repo:
mkdir my-desired-repo
cd my-desired-repo
git init
And you will need to make at least one commit. If the following three lines should be skipped, your first repo will appear immediate under your repo's root:
touch a_file_and_make_a_commit # see user's feedback
git add a_file_and_make_a_commit
git commit -am "at least one commit is needed for it to work"
With the temp file commited, merge
command in later section will stop as expected.
Taking from user's feedback, instead of adding a random file like a_file_and_make_a_commit
, you can choose to add a .gitignore
, or README.md
etc.
4 - Merge apps repo first:
git remote add apps-repo ../apps-repo
git fetch apps-repo
git merge -s ours --no-commit apps-repo/master # see below note.
git read-tree --prefix=apps -u apps-repo/master
git commit -m "import apps"
Now you should see apps directory inside your new repository. git log
should show all relevant historical commit messages.
Note: as Chris noted below in the comments, for newer version(>=2.9) of git, you need to specify --allow-unrelated-histories
with git merge
5 - Merge libs repo next in the same way:
git remote add libs-repo ../libs-repo
git fetch libs-repo
git merge -s ours --no-commit libs-repo/master # see above note.
git read-tree --prefix=libs -u libs-repo/master
git commit -m "import libs"
Continue if you have more than 2 repos to merge.
Reference: Merge a subdirectory of another repository with git