Post
Consolidating multiple git repositories into a single one
Today I decided to reorganize my GitHub repository. Here is a simple guide and automation command line to consolidate multiple repositories into a single one while retaining commit history.
Today I decided to reorganize my GitHub repository. In the past, I added a lot of learning projects there and as time passed by, it was growing considerably. Since I currently have some work-related projects there, I thought it deserved some reorganization.
My idea was to consolidate some of the larger projects I had into a single repository where each component is a sub-module of the project. This was a rather natural choice because most of those projects are written in Java and use Maven, therefore being very easy to refactor into a module-based project layout.
One requirement I had was that I wanted to retain the commit history. After looking at some solutions involving git's submodule or subtree features, I found one that was fairly simple. The process couldn't be simpler: you have to add the projects as remote to your new project and run a couple of commands to merge, move and commit the files.
Additionally, since my projects usually follow the same standard, it was quite simple to automate. After running the initial steps to configure the new (consolidated) repository, I was able to run the mass import using the following command line:
for module in $(echo "project1 project2") ; do git merge -m "Merged $module master for repository reorganization" $module/master ; mkdir $module ; (for file in $(echo "README.md pom.xml src test") ; do [[ -e $file ]] && git mv $file $module/ ; done) ; git status ; git commit -m "UPD: moved $module old files from a separate repo to a separate module" . ; done
I was even able to import the old branches for each of the sub-components I wanted to merge.