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)