subtrees in Git: How to split Directories into individual standalone repositories
Problem
You start working on an idea without having to bother if the project needs to split into smaller components, projects. Once the vision starts becomes clear and the horizon becomes clear, it makes sense to start thinking into managing components as their own standalone software repositories.
One does not want to put a lot of effort towards this migration, and not mess the History your git
repository has. Credit from your
colleagues should be manifested fairly in the new repositories.
How does one achieve this?
Current Structure
$ cd big-repository
# MEGA-REPOSITORY
.
├── SubProject1 # this should be a standalone repository
│ └── SubModule1
└── SubProject2 # this should be a standalone repository
├── SubModule1
├── SubModule2
└── tests
Solution
The great thing about git
is it diversified eco-system. The problem of making directories their own git repositories is something a large
pool of IT Developers face. The solution is to make the subprojects into a subtree
.
Usage
$ git subtree split --prefix <your_directory_name> --branch <branch_name>
here branch_name
can be whatever you wish it to be, your_directory_name
here in my case is SubProject1
and SubProject2
.
$ git subtree split --prefix SubProject1 --branch micro-project
git will make a branch called micro-project
and check it out for you. Upon listing the files and directories it will only show the content of
the directory mentioned (here, contents of SubProject1
)
Similarly for SubProject2
$ git subtree split --prefix SubProject2 --branch nano-project
Setting Up Repositories
Initialize your Repositories on your Git Server / Platform (GitHub, GitLab etc.)
Create a new directory
$ mkdir micro-project/ && cd micro-project/
Initialize the directory as a git repository
$ git init
Pull the subtree branch in the
big-repository
to the current directory$ git pull ../path/to/big-repository <branch-name>
In our case for
micro-project
the branch name ismicro-project
$ git pull ../path/to/big-repository micro-project
You can verify if all the commit logs are still intact in the new repository using:
$ git log --oneline
Add a
remote
to your new Repository using:$ git remote add origin <YOUR_GIT_SERVER_URL/micro-project.git>
Push to code to the Server:
$ git push -u origin master
Continue the steps for nano-project
too.