Skip to content
$ lynuxpro
← Git

LESSON 20 OF 21 · EXPERT · 18 MIN

Git at scale: monorepos and performance

HOW IT WORKSKeeping a monorepo fast

Monorepomany servicesPartial clone--filter=blob:noneSparse checkoutcone modefsmonitor + untracked cachefast statusgit maintenancebackground gc, commit-graphCIbuild only what changed
1/5

Big companies keep many services in one repository: one history, atomic cross-service changes.

As organisations grow, two questions come up: should we keep all our code in one repository or many, and how do we keep Git fast when a repository has millions of commits and hundreds of thousands of files? This lesson compares monorepos and polyrepos, then covers the concrete settings and techniques that keep large repositories fast, and how CI builds only what changed.

In simple words

  • Monorepo: one repository holding many projects or services.
  • Polyrepo: a separate repository for each project.
  • Affected builds: building and testing only the parts that changed, so a monorepo's CI stays fast.

Monorepo or polyrepo?

Monorepo (one repository)Polyrepo (one per service)
Cross-service changeone atomic commit and merge requestseveral coordinated MRs and releases
Shared codeused directly, always the latest versionpublished as versioned packages
Visibilityeveryone sees everythingaccess per repository
Tooling needsmust scale: sparse checkout, targeted CI, build toolssimple per repository
CImust detect what changedeach repository has its own pipeline
OwnershipCODEOWNERS per folderper repository

Google, Meta and Microsoft use enormous monorepos; many companies prefer one repository per service, or a hybrid (a monorepo per product or per team). Neither is "right". What matters is that the tooling matches the choice. A monorepo without targeted CI and ownership rules becomes slow and chaotic; a polyrepo without good package versioning drowns in coordinated releases.

A typical monorepo layout

platform/├── services/│   ├── payments/│   ├── checkout/│   └── catalog/├── libs/│   └── common/├── infra/│   ├── terraform/│   └── helm/├── .gitlab-ci.yml└── CODEOWNERS
OUTPUTterminalutf-8 · 12L

CODEOWNERS assigns required reviewers per path, so the payments team must approve changes under services/payments/:

/services/payments/   @acme/payments-team/infra/terraform/     @acme/platform-team*.tf                  @acme/platform-team
OUTPUTterminalutf-8 · 3L

Building only what changed

The key to monorepo CI is running jobs only for the parts a merge request touched. The building block is a diff of file names against the target branch:

git config --global user.name "Leela Kumar"git clone https://gitlab.com/acme/shop.gitcd shopgit diff --name-only HEAD~3 HEADgit diff --name-only HEAD~3 HEAD | cut -d/ -f1 | sort -u
SHELLbashutf-8 · 5L
.gitlab-ci.ymlapp.pypricing.pyrequirements.txt.gitlab-ci.ymlapp.pypricing.pyrequirements.txt
OUTPUTterminalutf-8 · 8L

In a real monorepo, cut -d/ -f1-2 would give services/payments, services/checkout and so on, and a script could decide which services to build. In merge request pipelines you compare with the merge base: git diff --name-only origin/main...HEAD (three dots).

GitLab CI supports this declaratively with rules: changes::

payments-test:  script: make -C services/payments test  rules:    - changes:        paths: [services/payments/**/*, libs/common/**/*]        compare_to: refs/heads/main
OUTPUTterminalutf-8 · 6L

Larger monorepos use build systems that understand dependencies and cache results (Bazel, Nx, Turborepo, Pants, Gradle), so a change in libs/common rebuilds exactly the services that depend on it.

Keeping Git itself fast

Git commands slow down when the working tree has hundreds of thousands of files, or history has millions of objects. These settings help on developer machines:

git config core.fsmonitor truegit config core.untrackedCache truegit config feature.manyFiles truegit maintenance start
SHELLbashutf-8 · 4L
  • core.fsmonitor: a filesystem watcher tells Git which files changed, so git status does not scan every folder. Built-in on macOS and Windows.
  • core.untrackedCache: remembers which folders had no untracked files.
  • feature.manyFiles: enables a group of settings tuned for large working trees (including a faster index format).
  • git maintenance start: schedules background jobs that prefetch from remotes, repack objects, and update the commit-graph file, which makes git log --graph, merge-base calculations and reachability checks much faster.

Measure before and after with time git status; on large repositories the difference can be seconds.

Clone and fetch strategies

git clone --filter=blob:none URL               # partial clone: blobs on demandgit clone --filter=blob:none --sparse URL      # plus sparse checkoutgit sparse-checkout set services/paymentsgit clone --depth 50 URL                       # CI: limited historygit fetch --prune                              # keep remote branches tidy
SHELLbashutf-8 · 5L

Scalar (shipped with Git since 2.38) sets all of these up in one command for very large repositories: scalar clone URL.

Server and CI side

  • Use shallow or partial clones in CI (GitLab: GIT_DEPTH, GIT_STRATEGY: fetch to reuse a cached checkout).
  • Cache dependencies and build outputs between pipeline runs.
  • Keep large binaries in LFS or an artifact registry.
  • Housekeeping on the Git server (GitLab runs repository housekeeping automatically) keeps packs optimised.

Keeping history healthy

  • Delete merged branches (GitLab can do it on merge) and prune stale ones.
  • Avoid committing generated files; they bloat history and conflict constantly.
  • Squash noisy commits before merging, so history and bisect stay useful.
  • Check what is large with git count-objects -vH, or find the biggest objects with git rev-list --objects --all | git cat-file --batch-check='%(objectsize) %(rest)' | sort -n | tail.

Real-world scenario: a monorepo pipeline that ran everything

A company merged 30 services into one repository. Every merge request ran all 30 test suites, taking 70 minutes, so developers batched changes into huge merge requests and the main branch was often red. They added rules: changes per service plus a dependency map for shared libraries, enabled partial clones and GIT_DEPTH: 20 on runners, and added CODEOWNERS. Typical pipelines dropped to 8 minutes, merge requests shrank, and each team reviewed only its own code.

Common mistakes

  • Moving to a monorepo without targeted CI, so every change builds everything.
  • No ownership rules, so anyone can change anything without the right reviewers.
  • Full clones in CI for every job.
  • Generated files and binaries in Git, which slow everything down over time.
  • Ignoring Git's performance settings on large repositories and blaming Git.

Practice

  1. List the files changed in the last three commits of the shop project with git diff --name-only.
  2. Turn that list into unique top-level folders with cut and sort -u.
  3. Write a CODEOWNERS file that gives two teams ownership of two folders.
  4. Enable core.fsmonitor, core.untrackedCache and git maintenance start in a repository.
  5. Write a GitLab CI job that runs only when files under services/payments/ change.

Cheat sheet

git diff --name-only origin/main...HEAD     changed files in an MRgit diff --name-only A B | cut -d/ -f1-2 | sort -ugit config core.fsmonitor truegit config core.untrackedCache truegit config feature.manyFiles truegit maintenance start                        background optimisationgit clone --filter=blob:none [--sparse] URLgit clone --depth N URLscalar clone URL                             large-repo defaultsCODEOWNERS                                   path → required reviewersrules: changes: paths:                        GitLab: run job only on changes
OUTPUTterminalutf-8 · 11L

Key takeaways

  • Monorepos make cross-cutting changes atomic but require scalable tooling; polyrepos are simpler per repository but need good versioning.
  • CI should build and test only what changed, using git diff --name-only or rules: changes.
  • fsmonitor, untrackedCache, feature.manyFiles and git maintenance keep large repositories fast.
  • Partial, sparse and shallow clones reduce what each developer and CI job downloads.