Using the Allow-Unrelated-Histories Flag to Merge Different Histories

Kanza SyedKanza Syed
2 min read

Git Documentation: https://git-scm.com/docs/git-merge#Documentation/git-merge.txt---allow-unrelated-histories

Understanding the Problem

Consider this use case: A company uses a microservice architecture to offer services in 25 domains. One of these services became very heavy due to extensive business requirements. The project manager decided to break down these requirements into smaller, manageable modules. The team realized they could split the service into three separate services. They are also working on new features for all three modules in an iterative and incremental way. The task now is to manage and sync their features so the team can efficiently switch to the relevant service without any rework or productivity loss.

Deriving the Solution

They discovered a very useful Git feature: Remote Tracking of branches using the --allow-unrelated-histories flag. They found that after creating and registering a new repository, they could easily sync the feature branches in just four simple steps.

Repository A became too large, so you want to split it into two further repositories B and C.

Prerequisites: Create a new repository B and create a temporary branch, for example, feature/sync-repoA-branchX.

Now execute the following set of commands:

  1. Add the remote repository: git remote add <repo-name> <path-to-remote-repo>

    Example: git remote add A ../A

  2. Fetch the remote repository: git fetch <repo-name>

    Example: git fetch A

  3. Merge the branches, allowing unrelated histories: git merge --allow-unrelated-histories <repo-name>/<branch-name>

    Example: git merge --allow-unrelated-histories A/master

    Note: The first commit may be large, but the process will become seamless after that, as both repositories will share a common base commit.

  4. Remove the remote repository: git remote remove <repo-name>

    Example: git remote remove A

Conclusion

Using the --allow-unrelated-histories flag in Git makes it much easier to split large repositories and keep everything in sync. This approach helps teams manage their projects more smoothly, avoiding unnecessary rework and keeping productivity high. It’s a handy trick for anyone dealing with complex, evolving codebases.

Please share your thoughts and feedback on this article to help us improve.

0
Subscribe to my newsletter

Read articles from Kanza Syed directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kanza Syed
Kanza Syed

A learner :-)