12. Git Local Mirror

12.1 Introduction

Generally, we divide our code into multiple repos. And, if we want to simultaneously work on two issues from same repo, we end up cloning that twice. When repo size grows even cloning starts consuming the disk space and network bandwidth. If we start having more repos, managing them becomes tough (think of backing up, replicating etc). However, There are better ways of organizing our code in git. Mono-repo can be used to pack a number of related repos into one, if not all repos of your organization.

Instead of creating too many repos, can I effectively club related source code into one repo (instead of multiple repos)? If so, how can I avoid, repeated clones? I want nearly all benefits of multi-repo in mono-repo. Let me try to explain why it is very possible and natural in mono-repo. It is much easier to mirror the git repos on local machine. Using work tree we can work on multiple issues for the same branch out of single repo. All will be explained in this. I tried to reduce the content as much as possible here...

12.2 Key Words/Concepts

Git Worktree, Mono-repo, git local mirror, Mono-repo branching conventions, having multiple revisions of a library with single application code and linking them at the will.

12.3 Git worktree and Mono Repo

This is about how effectively to use git worktree command.
When we checkout or switch a branch, git reads from its database and presents us a natural “Folder and files” of our source files. This folder structure is called ‘work tree’. Typically, it is created in the folder in which we cloned the repo. However, git can create a work tree outside the repo folder. And it can create one work tree for each branch any where in your local file system of computer!. Further, if you want to create multiple work trees for same branch in local file system, it is possible but not good idea. But, we can do creating new branches on top that branch.

Compare above with the cloning your repo multiple times for multiple tickets that you work con-currently. Think on these: cloning consumes your disc space, n/w bandwidth, tough to manage in our mind etc. On other hand work tree provides beautiful way of doing with single repo base. Following is brief introduction to worktree commands. You can find the better tutorials on internet on git worktree.

# cd to your repo folder
git worktree add <folder_worktree_to_create> <branch_commit>
# git worktree add ../my-feature feature/feature-a

This creates a folder ../my-feature and checkout the branch feature/feature-a in that folder. Thus we will have worktree of that branch in that folder.

# lists all worktrees, branches, folders of the repo, look for *, + sign
git worktree list
# to remove a worktree folder, do not use `rm` cmd. Instead use the below
git worktree remove <worktree_folder>
# or if you have deleted some worktree folders, use prune
git worktree prune

Note: You can create only one work tree per branch. If you try to create one more, git gives error. A simple solution to create more worktrees from same branch, just create some dummy branches out of the branch of your interest. And create work trees. Since all these branches will point to same content, hence no issue at all. I generally suffix _temp to branch name to identify from which branch I created the dummy branch. However, this is not needed generally.

12.4 Git mirror

Once we have a repo on our local machine, we can create as many work trees as we want without cloning same repo again and again. This local repo effectively starts acting as local git mirror. Still we will be pushing to remote on the need. To keep local repo updated, we would be doing git fetch/git pull as and when needed. We would never delete this local repo and we would never need to clone from remote again (just keep fetching/pulling).
If we really want to clone for some reasons, instead of cloning from remote, still we can create same clone from this local mirror repo. Here mono repo or small number of repos will be handy to mirror. Think, if we want to create a mirror, our job would be much easier if we have just one repo instead of 10 repos!

12.5 Multiple Lib. Rev.

Generally, we create a repo for a library code. Application users would just clone that lib repo and checkout which ever version of library they want. This is the flexibility of multi-repo.
In mono repo, we can create only one worktree per branch. If we really want to create multiple work trees for the same branch, we can employ a simple trick. Just create a temp branch out of the branch of our interest, and create a worktree for that temp branch. Do it again, if you want one more work tree. Also, there is an option of sparse checkout where, only particular dir inside the repo can be checkout.