DEV Community

Cover image for ๐Ÿ“ฆ Managing Multiple Projects in One Repository: Submodules, Subtrees, Monorepos & Partial Cloning Explained
K-kibet for Codespear

Posted on

๐Ÿ“ฆ Managing Multiple Projects in One Repository: Submodules, Subtrees, Monorepos & Partial Cloning Explained

Modern software development often involves several related projects that need to live togetherโ€”perhaps a shared library, a mobile app, an admin dashboard, and a backend API. Managing these independently while keeping them organized in a single place requires the right Git strategy.

In this guide, weโ€™ll explore four powerful approaches to organizing multiple projects under one repository:

  • Git Submodules
  • Git Subtrees
  • Monorepo structure
  • Partial cloning using sparse-checkout

Each technique has its strengths and ideal use cases. Letโ€™s break them down with clear examples.


๐Ÿ”ง 1. Git Submodules โ€” Independent Projects Inside a Parent Repository

Git submodules let you embed separate Git repositories inside a parent repository. Each submodule maintains its own commit history and remote origin.

โœ” When to Use Submodules

  • When projects must remain completely independent
  • When you want separate version control per project
  • When reused modules belong in multiple repositories

๐Ÿ“Œ Example Use Case

Main project: mega-suite
Submodules:

  • ui-engine
  • data-analyzer

๐Ÿ“ฅ Adding a Submodule

git submodule add https://github.com/example/ui-engine.git modules/ui-engine
git commit -m "Added ui-engine as submodule"
Enter fullscreen mode Exit fullscreen mode

This creates a .gitmodules file that tracks submodule entries.

โ–ถ Initialize & Update Submodules

git submodule update --init --recursive
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”„ Working Inside a Submodule

cd modules/ui-engine
git checkout main
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ก Pros

  • Clean separation
  • Reusable modules
  • Independent version history

โš  Limitations

  • Can confuse beginners
  • Requires extra commands to sync
  • Submodules can break if misconfigured

๐ŸŒณ 2. Git Subtrees โ€” Embed Projects Without Complexity

Git subtrees merge another repository into a folder inside your repoโ€”without the extra complexity of submodules.

โœ” When to Use Subtrees

  • When you want all code in one repo
  • But still want to pull updates from external repos
  • And you donโ€™t want the overhead of submodules

๐Ÿ“Œ Example Use Case

Main repository: mega-suite
Subtree projects:

  • ui-engine
  • data-analyzer

โž• Add a Subtree

git remote add ui-engine https://github.com/example/ui-engine.git
git subtree add --prefix=modules/ui-engine ui-engine main --squash
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”„ Updating the Subtree

git subtree pull --prefix=modules/ui-engine ui-engine main --squash
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ก Pros

  • Everything stored inside one repo
  • No special clone commands
  • Easier than submodules
  • No detached HEAD issues

โš  Limitations

  • Repo size grows faster
  • No true isolation between projects

๐Ÿข 3. Monorepos โ€” A Single Repo for Multiple Projects

A monorepo is a repository where all projects live togetherโ€”even if they are unrelated. Big companies like Google, Meta, and Microsoft use monorepos to streamline development.

โœ” When to Use a Monorepo

  • When projects depend on each other
  • When you want shared tooling and libraries
  • When you want centralized versioning

๐Ÿ“ Example Monorepo Structure:

mega-suite/
โ”œโ”€โ”€ apps/
โ”‚   โ”œโ”€โ”€ dashboard/
โ”‚   โ””โ”€โ”€ mobile/
โ”œโ”€โ”€ services/
โ”‚   โ”œโ”€โ”€ auth-service/
โ”‚   โ””โ”€โ”€ billing-service/
โ””โ”€โ”€ shared/
    โ”œโ”€โ”€ ui-kit/
    โ””โ”€โ”€ utils/
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ก Pros

  • Easy refactoring across multiple projects
  • Shared code lives in one place
  • One repository to maintain
  • Unified versioning

โš  Limitations

  • Repo can grow very large
  • Requires tooling (NX, Lerna, Turborepo)
  • Requires discipline to maintain structure

๐Ÿ“ฅ 4. Partial Cloning โ€” Clone Only the Project You Need

Partial cloning (via sparse-checkout) allows users to clone only specific folders instead of the entire repository. This is perfect for monorepos.

โœ” When to Use Sparse-Checkout / Partial Cloning

  • Repo is too large
  • Developers work on only one project
  • You want to save disk space and bandwidth

๐Ÿ“Œ Example Setup

Monorepo: mega-suite
Folders:

  • apps/dashboard
  • apps/mobile

๐Ÿงฉ Clone Repo Without Checkout

git clone --no-checkout https://github.com/example/mega-suite.git
cd mega-suite
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ง Initialize Sparse Checkout

git sparse-checkout init
git sparse-checkout set apps/dashboard
Enter fullscreen mode Exit fullscreen mode

โ–ถ Checkout Files

git checkout main
Enter fullscreen mode Exit fullscreen mode

Now only the dashboard project is downloaded!

๐Ÿ’ก Pros

  • Works perfectly with monorepos
  • Saves disk space
  • Developers download only what they need

โš  Limitations

  • Requires Git 2.25+
  • Adds a bit of setup complexity

๐ŸŽฏ Which Approach Should You Choose?

Use Case Best Method
Projects must remain fully independent Submodules
Want all code in one repo but keep external sources Subtrees
Multiple related apps/libraries in one place Monorepo
Only need to clone specific folders Partial cloning

๐Ÿš€ Final Thoughts

Organizing multiple projects under one Git repository isnโ€™t one-size-fits-all. Each methodโ€”submodules, subtrees, monorepos, and partial cloningโ€”serves a different purpose.

  • For strict independence โ†’ Submodules
  • For simpler integration โ†’ Subtrees
  • For large ecosystems โ†’ Monorepos
  • For large repos requiring selective cloning โ†’ Partial cloning

Pick the strategy that fits your workflow and scale your projects with confidence.

Top comments (0)