If you have multiple modules within a git repository, it’s only a matter of time for it to get bulky and clutter enough to make you want to refactor it and move some modules to their own separate repositories (for better maintainability). If that bulky repository has several contributors, it’s also critical to preserve contribution history (i.e commits, changes), even when you move it into several new repositories.
Recently I came up with such situation. There, I had to move several extension from a parent repo into their own separate repos as shown in the figure. And here’s how I did it;
- Fork the original repository into your account. Here, I have forked wso2/siddhi repo into grainier/siddhi
- Clone the forked repo into your local machine. Here, I cloned it into a new directory named “siddhi-execution-time” (I’m going to extract time extension first).
Shell1git clone https://github.com/grainier/siddhi.git siddhi-execution-time
- Go inside cloned directory, and remove all remote tracking from the cloned git repository.
Shell12git remote -vgit remote rm origin
- Now it’s time to filter and prune. Doing this will clean all the other directories as well as their git history, while only keeping files and history of filtered subdirectory.
Shell1git filter-branch --prune-empty --subdirectory-filter modules/siddhi-extensions/time master
It will result in something like this;
- Now, fix the project structure (fixing pom file, add a .gitignore, add other required modules etc..) as you want.
- Add new files to git and commit those changes (remember still you cannot push it since we haven’t set the remote tracking location yet).
12git add -Agit commit -m "Commit message"
- Now create a new git repo in GitHub (or any git provider), and copy it’s remote tracking location (mine is https://github.com/grainier/siddhi-execution-time.git). It doesn’t matter whether this is an empty repo or existing repo (with files).
- Now add that link as the remote tracking location to our local git repo.
Shell12git remote add origin https://github.com/grainier/siddhi-execution-time.gitgit remote -v
- [optional] If it’s not an empty repo, do a git pull and merge the content with our repository.
- Finally, push the restructured local repo to remote-tracking branch;
Shell1git push origin master
That’s it, you’re all done 🙂