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-enginedata-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"
This creates a .gitmodules file that tracks submodule entries.
โถ Initialize & Update Submodules
git submodule update --init --recursive
๐ Working Inside a Submodule
cd modules/ui-engine
git checkout main
๐ก 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-enginedata-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
๐ Updating the Subtree
git subtree pull --prefix=modules/ui-engine ui-engine main --squash
๐ก 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/
๐ก 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/dashboardapps/mobile
๐งฉ Clone Repo Without Checkout
git clone --no-checkout https://github.com/example/mega-suite.git
cd mega-suite
๐ง Initialize Sparse Checkout
git sparse-checkout init
git sparse-checkout set apps/dashboard
โถ Checkout Files
git checkout main
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)