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 change | one atomic commit and merge request | several coordinated MRs and releases |
| Shared code | used directly, always the latest version | published as versioned packages |
| Visibility | everyone sees everything | access per repository |
| Tooling needs | must scale: sparse checkout, targeted CI, build tools | simple per repository |
| CI | must detect what changed | each repository has its own pipeline |
| Ownership | CODEOWNERS per folder | per 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└── CODEOWNERSCODEOWNERS 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-teamBuilding 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.gitlab-ci.ymlapp.pypricing.pyrequirements.txt.gitlab-ci.ymlapp.pypricing.pyrequirements.txtIn 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/mainLarger 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 startcore.fsmonitor: a filesystem watcher tells Git which files changed, sogit statusdoes 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 makesgit 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 tidyScalar (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: fetchto 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 withgit 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
- List the files changed in the last three commits of the shop project with
git diff --name-only. - Turn that list into unique top-level folders with
cutandsort -u. - Write a
CODEOWNERSfile that gives two teams ownership of two folders. - Enable
core.fsmonitor,core.untrackedCacheandgit maintenance startin a repository. - 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 changesKey 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-onlyorrules: changes. fsmonitor,untrackedCache,feature.manyFilesandgit maintenancekeep large repositories fast.- Partial, sparse and shallow clones reduce what each developer and CI job downloads.