Skip to content

12. Working with Submodules & Large Repositories

Git submodules allow you to include another repository within your current project. This is useful when integrating external libraries or dependencies that are maintained separately but are required in your project.

To add a submodule to your project, run the following command:

Terminal window
git submodule add <submodule-url>

This adds the specified repository as a submodule, making it available within your project. You can then use its code, such as calling functions from the included repository.

Since a submodule is an independent repository, it may receive updates over time. To fetch the latest changes, navigate to the submodule’s directory and run:

Terminal window
git pull origin <main-branch>

This updates the submodule to the latest version from its remote repository.

Using Git submodules has several advantages:

  • ✅ Keeps dependencies separate: each submodule remains an independent Git repository, maintaining its own history and versioning.
  • ✅ Prevents unwanted updates: since submodules are locked to a specific commit, your project won’t break due to upstream changes.
  • ✅ Reduces duplication: shared libraries can be reused across multiple projects without copying files.

However, submodules also come with some challenges:

  • ❌ Complex workflows: updating a submodule requires committing and pushing changes in the submodule first, then updating and committing the reference in the main project, since Git tracks submodules at a specific commit.
  • ❌ Easy to forget updates: developers must manually update submodules, which can lead to inconsistencies.
  • ❌ Cloning issues: when cloning a repository with submodules, users need to run:
Terminal window
git submodule update --init --recursive

to ensure all dependencies are properly fetched.

While submodules are great for keeping external dependencies organized, they can be tricky to manage. If your project relies on frequent updates to shared code, using a package manager might be a smoother and less hassle-prone option.

When working on projects that involve large files, such as big datasets or media assets, you might run into file size limits imposed by your remote repository hosting platform. To address this, Git provides Large File Storage (LFS), an extension designed to handle large files efficiently without bloating your repository.

Remember from Chapter 7 that Git stores a snapshot of your files at every commit. For text files this is fine, but for a 200 MB video that changes a few times, Git ends up keeping every version forever inside .git. Over time the repository becomes huge, and every clone has to download that entire history. LFS solves this by storing a tiny text pointer in your repository instead of the file itself, while the actual content lives on a separate LFS server.

Git LFS is a separate tool, so you first need to install it from git-lfs.com, then enable it once per machine:

Terminal window
git lfs install

This adds the hooks and filters Git needs to redirect large files to LFS.

Next, tell LFS which file patterns it should manage. This is best done before committing those files:

Terminal window
git lfs track "*.psd"
git lfs track "*.mp4"

These commands update a .gitattributes file, which you must commit so the rest of your team uses the same rules:

Terminal window
git add .gitattributes
git commit -m "Track large media files with Git LFS"

From now on, any matching file you add and commit is stored as a lightweight pointer, while LFS uploads the real content during git push.

To see which files are currently handled by LFS, run:

Terminal window
git lfs ls-files

Migrating files that are already in history

Section titled “Migrating files that are already in history”

If large files were committed before you set up LFS, they’re already baked into your history, and only rewriting history can move them. Git LFS provides a command for that:

Terminal window
git lfs migrate import --include="*.psd" --everything

As a project grows over months or years, its .git directory accumulates a lot of objects, and some operations can start to feel slower. Git includes built-in maintenance tools to keep things fast.

Git stores each new object separately at first (these are called “loose” objects). The git gc command compresses them into packfiles, prunes unreachable objects that are older than a grace period (2 weeks by default, so very recent ones are kept as a safety net), and tidies up references:

Terminal window
git gc

Git runs a light version of this automatically from time to time, but you can trigger it manually after large operations. For a more thorough cleanup, use:

Terminal window
git gc --aggressive --prune=now

Modern Git can schedule maintenance to run quietly in the background so you never have to think about it:

Terminal window
git maintenance start

This keeps the repository optimized over time without interrupting your workflow.

When you only need the latest state of a big repository (for example on a CI server), you don’t have to download its entire history. A shallow clone fetches only the most recent commits:

Terminal window
git clone --depth 1 <remote-repository-url>

This dramatically reduces clone time and disk usage. You can also use a partial clone to defer downloading file contents until they’re actually needed:

Terminal window
git clone --filter=blob:none <remote-repository-url>

Both shallow and partial clones rely on server support, which the major hosting platforms provide.

In a large monorepo you may only care about a handful of directories. Sparse-checkout populates just those in your working tree and leaves the rest out, which keeps everyday commands fast:

Terminal window
git sparse-checkout set <dir1> <dir2>

Paired with a partial clone, this is how teams stay productive in repositories with millions of files.

GoalCommand
Compress and clean up objectsgit gc
Aggressive one-off cleanupgit gc --aggressive --prune=now
Automatic background upkeepgit maintenance start
Clone only recent historygit clone --depth 1 <url>
Clone without downloading blobsgit clone --filter=blob:none <url>
Check out only some directoriesgit sparse-checkout set <dir>

Between submodules, LFS, and these maintenance commands, you now have the tools to keep even a large, long-lived repository fast and manageable.