Skip to main content

Command Palette

Search for a command to run...

Using git-monorepo to merge existing Git repositories into a monorepo

Updated
3 min read

In this article, we will look at how to use a tool I built named git-monorepo to merge existing Git repositories into a monorepo while maintaining the complete history from each repository. This can be useful if you want to reduce repository-sprawl and manage all your projects in one repository, or if you need to perform offsite backup of your code bases for example.

What is a monorepo?

A monorepo is a workspace that contains multiple projects in one Git repository. According to Semaphore CI "A monorepo is a version-controlled code repository that holds many projects. While these projects may be related, they are often logically independent and run by different teams."

Why would I want to merge my repos?

Adopting monorepos can provide several advantages. Firstly, code sharing and reuse become effortless. Developers on your team can easily find and incorporate existing libraries or functionalities into new projects, reducing redundancy and development time.

Monorepos can also enhance collaboration by fostering a shared codebase as teams gain better visibility into interdependent projects built by other teams, enabling faster identification of potential issues and more streamlined enforcement of coding practices and standards across the entire codebase. This can result in better code quality, easier to apply organization wide standards and practices which may help reducing maintenance overhead in the long run.

Another use case is for performing backups of organization wide codebases. This may be ideal for situations where disaster recovery, compliance or regulatory requirement insist that you keep an offsite backup. Being able to merge the code periodically, and download or upload to some Object Storage is a good use case for merging multiple repositories into a monorepo.

Installing and using git-monorepo

Firstly, you will need to install git-monorepo. More documentation on the tool can be found here

Download the latest release from GitHub: https://github.com/zikani03/git-monorepo/releases

After installation, run the following example command to create a monorepo from two GitHub repositories. This may take a bit of time as the repositories have to be cloned from GitHub to your machine, and then the commit histories replayed one-by-one.


$ git-monorepo init \
    --sources gh:zikani03/git-monorepo,gh:zikani03/articulated \
    --target my-new-monorepo

After the command completes, we can inspect the new repository using Git as usual. You will see that the history is now intertwined as if the two repos had been one from the start!

$ cd my-new-monorepo
$ git log --oneline

In the example above you can see we use the gh shorthand for GitHub to indicate that the repositories we are using are going to be pulled from GitHub. You can also specify the full URL to the repository and the tool will continue to work :


$ git-monorepo init \
    --sources https://github.com/zikani03/git-monorepo,https://github.com/zikani03/articulated \
    --target my-new-monorepo

It also works with other Git hosting services

git-monorepo works well with GitHub but you can also use it clone repositories from any Git server. There are shorthands for GitHub (gh), BitBucket (bb) and GitLab (gl). Small caveat is that we have yet to test thoroughly with private repository servers.

Here is an example of cloning projects from GitLab and Codeberg hosting services:

$ git-monorepo init \
    --sources https://gitlab.com/writeonlyhugo/up-business-theme,https://codeberg.org/201984/dut \
    --target my-new-monorepo

Conclusion

In this article I shared a tool I built named git-monorepo which can be used to merge repositories into a monorepo. Comments and contributions are always welcome!