読み物Git

Level: Pro

Managing Large Codebases: Monorepos, Git LFS, Submodules, and Worktrees

Keep big repositories fast and manageable: monorepo trade-offs, partial clones, sparse checkout, background maintenance, Git LFS, submodules, and worktrees.

Edeeson Opina9 min read

A Pro lesson card: lesson 14 of the Git & Version Control series, Managing Large Codebases: Monorepos, Git LFS, Submodules, and Worktrees, with the Miso fox waving.

What you’ll learn

  • Weigh monorepos against separate repositories
  • Use partial clones and cone-mode sparse checkout
  • Keep large repositories fast with maintenance and fsmonitor
  • Store large files with Git LFS
  • Use submodules and worktrees appropriately
On this page
  1. Before you start
  2. Monorepo or several repositories?
  3. CI in a monorepo
  4. Getting less data: partial and shallow clones
  5. Checking out less: sparse checkout
  6. Keeping it fast: maintenance
  7. Large files: Git LFS
  8. Sharing code between repositories: submodules
  9. Several branches at once: worktrees
  10. Common mistakes and troubleshooting
  11. Practical exercise: tame a big repository
  12. Key takeaways
  13. References
  14. What's next

Git handles small and medium projects effortlessly. As a codebase grows, with years of history, hundreds of thousands of files, several applications in one repository, or large binary assets, the defaults start to hurt: clones take ages, git status is slow, and the repository grows by gigabytes. This lesson covers how to organize large codebases (monorepos versus several repositories), and the Git features that keep them manageable: partial and shallow clones, sparse checkout, background maintenance, Git LFS for large files, submodules for shared code, and worktrees for working on several branches at once.

Before you start

You should understand objects and packfiles from Understanding Git Internals: Objects, References, Packfiles, and History. The examples run on any recent Git; Git LFS is a separate download.

Monorepo or several repositories?

A monorepo keeps many projects (for example a web app, a desktop app, shared UI packages, and documentation) in one repository. A polyrepo setup gives each its own repository.

A monorepo makes some things easy:

  • Atomic changes across projects: change a shared package and every app that uses it in one reviewed commit.
  • One version of shared code: no publishing internal packages just to use them in a sibling project.
  • Consistent tooling: one CI setup, one set of lint and format rules.
  • Visibility: anyone can find and search all the code.

It also creates costs:

  • Scale: more files and history mean slower Git operations without the techniques below.
  • CI: every change can trigger every project's tests unless the pipeline knows which projects a change affects.
  • Access control: Git permissions apply to whole repositories; restricting part of a monorepo needs platform features such as code owners, or separate repositories.
  • Tooling: a build system that understands project boundaries (for example workspaces in pnpm or Cargo, or tools like Nx, Turborepo, or Bazel) becomes important.

Several repositories give clear ownership, independent release cycles, and fine-grained permissions, at the cost of coordinating changes across repositories and versioning shared code.

Neither is right in general. Teams that change shared code often and want one consistent toolchain lean toward monorepos; independent products with separate teams and release cycles lean toward separate repositories.

CI in a monorepo

The most immediate monorepo cost is CI time. Run each project's jobs only when its files (or a shared dependency) change. Most CI systems support path filters:

YAML
# GitHub Actions or Forgejo Actions: run this workflow only for web app changes
on:
  pull_request:
    paths:
      - "apps/web/**"
      - "packages/ui/**"

GitLab offers the same with rules: changes:. For larger monorepos, build tools that compute the affected projects from the dependency graph are more reliable than hand-maintained path lists, because a change to a shared package affects every project that uses it.

Getting less data: partial and shallow clones

A normal clone downloads every version of every file. Often you don't need that.

A partial clone downloads all commits and trees, but fetches file contents (blobs) only when you need them:

Shell
git clone --filter=blob:none git@code.your-company.test:team/mono.git

History commands like git log stay fast and complete, and older file versions are downloaded on demand, for example by git blame or checking out an old commit. The server must support it; most major platforms do. Treeless clones (--filter=tree:0) go further and suit throwaway CI builds better than daily development.

A shallow clone cuts history instead:

Shell
git clone --depth 1 git@code.your-company.test:team/mono.git

You get only the latest commit. It's fast for CI jobs that only build the current version, but many commands (log, blame, merges with old branches) can't work properly without history. For development machines, prefer partial clones.

Checking out less: sparse checkout

Sparse checkout keeps the full repository but populates only the directories you work on. In a monorepo with apps/web, apps/desktop, packages/ui, and docs, a web developer can check out just what they need:

Shell
$ git clone --no-checkout git@code.your-company.test:team/mono.git
$ cd mono
$ git sparse-checkout set --cone apps/web packages/ui
$ git checkout main
$ find . -name README.md -not -path './.git/*'
./apps/web/README.md
./packages/ui/README.md

Cone mode (--cone, the default for sparse-checkout set) works with whole directories, which is much faster than arbitrary patterns. Files at the repository root are always included. Combine sparse checkout with a partial clone, and you download and check out only what you use.

Add more directories later with git sparse-checkout add docs, or return to a full checkout with git sparse-checkout disable.

Keeping it fast: maintenance

Large repositories benefit from background housekeeping:

Shell
git maintenance start

This registers the repository for scheduled tasks: prefetching from remotes, updating the commit-graph file (which speeds up history walks), packing loose objects, and more. On large working trees, the built-in file system monitor (git config core.fsmonitor true, available on Windows and macOS) makes git status avoid scanning every file. Scalar, included with Git, configures all of this for very large repositories in one command (scalar clone or scalar register).

Large files: Git LFS

Git stores every version of every file. For a 200 MB video edited ten times, that's up to 2 GB of history that every clone downloads. Git LFS (Large File Storage) replaces large files in the repository with small text pointers, and stores the actual content on a separate LFS server, downloading only the versions you check out.

Shell
git lfs install                       # once per machine
git lfs track "*.psd" "*.mp4"         # writes rules to .gitattributes
git add .gitattributes
git add design/hero.psd
git commit -m "Add hero design"

The rules live in .gitattributes, which must be committed so every clone treats the files the same way:

.gitattributesText
*.psd filter=lfs diff=lfs merge=lfs -text
*.mp4 filter=lfs diff=lfs merge=lfs -text

What's committed for each file is a short pointer containing a version line, the SHA-256 of the content, and its size; the content itself goes to LFS storage when you push.

Things to know before adopting LFS:

  • Set it up early. Tracking a pattern doesn't move files already in history. Converting existing history needs git lfs migrate import, which rewrites history (lesson 9's warnings apply).
  • Storage and bandwidth limits. Hosting platforms meter LFS storage and transfer separately from repository size; check your plan.
  • Everyone needs LFS installed, or they get pointer files instead of content.
  • LFS isn't a substitute for an artifact store. Build outputs, datasets, and release binaries often belong in a package registry or object storage, not in the repository at all.

Sharing code between repositories: submodules

A submodule embeds another repository at a fixed commit inside yours:

Shell
git submodule add https://code.example-project.test/libs/shared-ui.git vendor/shared-ui
git commit -m "Add shared-ui as a submodule"

The parent repository records the submodule's URL in .gitmodules and the exact commit it uses. Cloning requires an extra step to fetch submodule contents:

Shell
git clone --recurse-submodules git@code.your-company.test:team/app.git
# or, in an existing clone:
git submodule update --init --recursive

Submodules are precise (you always know which version of the dependency you use), but they're easy to get wrong: forgetting to initialize them, committing a parent change without pushing the submodule's commit, or ending up on a detached HEAD inside the submodule. Use them when you truly need to pin another repository's source. For most shared libraries, a package manager (npm, Cargo, Composer) is simpler. Subtree merging (git subtree) is an alternative that copies the other repository's files into yours, avoiding the extra step for users at the cost of a more complex update process.

Several branches at once: worktrees

Switching branches changes your working tree, which is disruptive in a large project with a running dev server or a long build. Worktrees let one repository have several working directories, each on its own branch, sharing the same object database:

Shell
$ git worktree add ../mono-hotfix -b hotfix/login main
Preparing worktree (new branch 'hotfix/login')
HEAD is now at 436efa2 init

$ git worktree list
/home/ada/code/mono         436efa2 [main]
/home/ada/code/mono-hotfix  436efa2 [hotfix/login]

Fix the bug in ../mono-hotfix while your feature work stays untouched in the main folder. When you're done:

Shell
git worktree remove ../mono-hotfix

Worktrees are cheaper than a second clone because objects are shared, and safer than stashing because nothing is set aside. A branch can be checked out in only one worktree at a time.

Common mistakes and troubleshooting

  • Committing large binaries before deciding on LFS. They stay in history forever unless you rewrite it. Decide early.
  • Shallow clones for development. Many commands misbehave without history. Use partial clones instead.
  • Forgetting --recurse-submodules. Submodule folders appear empty. Run git submodule update --init --recursive.
  • Pushing a parent that points at an unpushed submodule commit. Others can't check it out. Push the submodule first; git push --recurse-submodules=check catches it.
  • Running every CI job for every change in a monorepo. Use path filters or a build tool that computes affected projects.
  • "Pointer file" content instead of the real file. Git LFS isn't installed; run git lfs install and git lfs pull.
  • Deleting a worktree folder by hand. Use git worktree remove, or run git worktree prune afterwards.

Practical exercise: tame a big repository

  1. Create a practice monorepo with apps/web, apps/desktop, packages/ui, and docs, each with a few files and commits.
  2. Clone it with --filter=blob:none and --no-checkout, then use cone-mode sparse checkout to get only apps/web and packages/ui. Add docs later.
  3. Run git maintenance start in a real large repository you work on, and compare git status timing before and after enabling core.fsmonitor.
  4. Install Git LFS, track *.png, commit an image, and look at what's actually committed with git show HEAD.
  5. Create a worktree for a hotfix branch, commit there, merge it, and remove the worktree.
  6. Write down whether your current projects would be better as a monorepo or separate repositories, and why.

Key takeaways

  • Monorepos simplify cross-project changes and tooling; separate repositories simplify ownership, permissions, and independent releases.
  • Partial clones (--filter=blob:none) and cone-mode sparse checkout let you download and check out only what you need.
  • Background maintenance, commit-graph, fsmonitor, and Scalar keep large repositories fast.
  • Git LFS keeps large binary files out of Git history; adopt it early and mind storage limits.
  • Submodules pin other repositories precisely but add steps; package managers are often simpler.
  • Worktrees give you several branches checked out at once from one repository.

References

What's next

The final lesson is about keeping your repositories, and your options, safe in the long run. Git Migration, Mirroring, Backup, and Avoiding Vendor Lock-In shows how to move between platforms and keep independent copies.

See every lesson in Git & Version Control: From Fundamentals to Advanced Workflows