Breaking Down Microservices from a Unified Git Repository

Scenario

Assume that we have 100 micro-services in a single repository. Developers have been working for three years, and there have been more than 2000 commits made in that single repository. Now, considering the best practices of DevSecOps, they want to split each single microservice into a repository.

Resolution

Step 1: Clone the parent repository in your local directory using a terminal.

git clone <parent-repo-url>

Step 2: You have to install a git-filter package in your local environment to achieve this because the native git commands will not support it.

Step 3: Install the package using the below commands

For yum packages,

sudo yum install python3-pip
pip3 install git-filter-repo
git filter-repo --version

For apt packages,

sudo apt-get install git-filter-repo

After executing these commands, you will be able to see the git filter-repo command working in your terminal

Step 4: Navigate to your cloned directory, execute the below command, and replace the path with the actual single micro-service path in your repository.

git filter-repo --path <your-path>

for example, if login is a service and it contains a directory, then

git filter-repo --path login/

This will remove additional folders or services from your local repository and gather all of those specific micro-service commits.

Step 5: Create a new repository and copy the git url (clone url)

Step 6: Set the remote repository URL in your local terminal, like in the below command.

git remote add <service-name> <new-repo-url>

Step 7: After adding the remote URL, push the code using the below command.

git push <service-name> <branch-name>

Replace the branch name with either master or main according to your git repository

Conclusion

Now, all of the commits for that specific service, along with your single microservice, are pushed to a new git repository. For each of your micro-services, repeat the procedure. Start the new procedure after removing the local clone of the parent repository.

Did you find this article valuable?

Support Jayakumar Sakthivel by becoming a sponsor. Any amount is appreciated!