Git is designed for source code: many small text files that change line by line. Real projects stretch that model: shared libraries used by several repositories, machine-learning models and design assets of hundreds of megabytes, and monorepos with tens of thousands of files where you work on just one folder. This lesson covers the three tools Git offers for those situations (submodules, Git LFS and sparse checkout with partial clone), what each is good for, and the trade-offs that bite teams who adopt them without thinking.
In simple words
- Submodule: another Git repository placed inside yours, pinned to an exact commit.
- Git LFS: stores big files (videos, models, datasets) outside normal Git history, leaving small pointers in the repo.
- Sparse checkout: only puts the folders you need on disk.
- Partial clone: downloads file contents only when needed, so huge repos clone quickly.
Set up
git config --global user.name "Leela Kumar"git clone https://gitlab.com/acme/shop.gitcd shopSubmodules: another repository inside yours
A submodule embeds another Git repository at a specific folder, pinned to an exact commit. Typical uses: a shared library, a vendored dependency, common CI templates or Terraform modules used by several projects.
git submodule add https://gitlab.com/acme/common.git libs/commoncat .gitmodulesgit status -sCloning into '/home/lynuxpro/shop/libs/common'...remote: Enumerating objects: 42, done.Receiving objects: 100% (42/42), 8.10 KiB | 8.10 MiB/s, done.[submodule "libs/common"] path = libs/common url = https://gitlab.com/acme/common.gitA .gitmodulesA libs/commonTwo things are recorded in your repository:
.gitmodules: the path and URL of each submodule.- A special entry (a "gitlink") at
libs/commonthat stores the commit hash the submodule is pinned to, not its files.
git commit -m "Add common library as a submodule"git submodule statusWorking with submodules
git clone --recurse-submodules URL # clone including submodulesgit submodule update --init --recursive # after a plain clone, or after pulling a new pingit -C libs/common switch main && git -C libs/common pull # move the submodule forwardgit add libs/common && git commit -m "Bump common to latest" # record the new pingit submodule foreach 'git status -s' # run a command in every submoduleTrade-offs
Submodules give exact, reproducible pins, but they are a frequent source of confusion: a plain git clone leaves the folders empty, git pull does not update them automatically, and changes inside a submodule must be committed and pushed in the submodule first, then the new pin committed in the parent. Before reaching for submodules, consider a package (pip, npm, Maven, a container image, a Terraform registry module): versioned artifacts are usually simpler for consumers. GitLab CI's include: and versioned Terraform module sources often replace submodules entirely.
Git LFS: large files outside Git
Git stores every version of every file forever. Commit a 500 MB model ten times and every clone downloads 5 GB. Git Large File Storage (LFS) keeps large binary files on a separate LFS server and stores only a tiny pointer file in Git:
version https://git-lfs.github.com/spec/v1oid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393size 524288000When you check out, LFS downloads the real content for the versions you need.
git lfs installgit lfs track "*.onnx"cat .gitattributesgit add .gitattributesgit commit -m "Track ONNX models with Git LFS"Updated Git hooks.Git LFS initialized.Tracking "*.onnx"*.onnx filter=lfs diff=lfs merge=lfs -textgit lfs track writes a rule to .gitattributes, which you must commit, so every clone knows which files go through LFS. After that you git add and commit large files normally.
git lfs ls-files # files stored in LFSgit lfs pull # download LFS content for the current checkoutgit lfs migrate import --include="*.psd" --everything # move existing history into LFS (rewrites history!)GIT_LFS_SKIP_SMUDGE=1 git clone URL # clone pointers only, fetch big files laterGood candidates: ML models and datasets, images, videos, design files, compiled binaries that must be versioned. LFS storage and bandwidth are usually billed on GitLab and GitHub, so ignore files that do not need versioning at all (build output belongs in an artifact registry, not Git).
Sparse checkout and partial clone: only what you need
In a big monorepo you may work on one service out of hundreds. Two features combine to make that fast:
- Partial clone (
--filter=blob:none) downloads all commits and trees but fetches file contents (blobs) only when they are needed. Cloning becomes much faster and smaller while history commands still work. - Sparse checkout limits the working tree to the folders you choose, so
git statusand your editor only deal with those files.
git clone --filter=blob:none --sparse https://gitlab.com/acme/platform.gitcd platformgit sparse-checkout set services/payments libs/commongit sparse-checkout set services/payments libs/commongit sparse-checkout listservices/paymentslibs/commonUseful commands:
git sparse-checkout add services/checkout # add another foldergit sparse-checkout disable # back to the full working treegit clone --depth 1 URL # shallow clone: only the latest commit (CI)git fetch --unshallow # get the full history laterCone mode (the default since Git 2.37) restricts patterns to whole folders, which keeps sparse checkout fast even in enormous repositories.
Shallow clones in CI
CI jobs rarely need history. git clone --depth 1 (GitLab's GIT_DEPTH variable does this for you) makes checkout much faster. Increase the depth, or fetch more, for jobs that need history, such as git describe, changelog generation or diffing against main.
Choosing the right tool
| Problem | Tool |
|---|---|
| Reuse code from another repository at an exact version | submodule, or better a versioned package |
| Big binary files that must be versioned | Git LFS |
| Huge repository, you need only a few folders | sparse checkout + partial clone |
| CI checkout too slow | shallow clone (--depth) or partial clone |
| Build output, logs, dependencies | not Git: artifact registry, .gitignore |
Real-world scenario: the 9 GB repository
A data team committed trained models directly into Git for two years. Clones took 40 minutes and CI runners kept running out of disk. They moved models to Git LFS with git lfs migrate import --include="*.pt,*.onnx" --everything, coordinated a one-time force push and re-clone with everyone, set CI to GIT_DEPTH: 20 and fetched LFS files only in jobs that needed them. Clone time dropped to under two minutes. Going forward, a pre-commit hook blocked any non-LFS file over 10 MB.
Common mistakes
- Cloning without
--recurse-submodulesand wondering why folders are empty. - Committing in a submodule but forgetting to push it, so the parent points at a commit nobody else can fetch.
- Forgetting to commit
.gitattributesaftergit lfs track; teammates then commit the real binaries. - Adding LFS after the fact without migrating: the old large blobs are still in history.
- Shallow clones in jobs that need history, breaking
git describeor diffs.
Practice
- Add a submodule to the shop project and inspect
.gitmodulesandgit status -s. - Track
*.onnxwith Git LFS and commit.gitattributes. - Set a sparse checkout to two folders and list it.
- For each of these, pick a tool: a shared Helm chart library, a 2 GB video asset, a 40,000-file monorepo, a slow CI checkout.
Cheat sheet
git submodule add URL path add, pinned to a commitgit clone --recurse-submodules URLgit submodule update --init --recursivegit submodule status | foreach 'cmd'git lfs install | track "*.ext" | ls-files | pullgit lfs migrate import --include="*.ext" --everythinggit clone --filter=blob:none --sparse URLgit sparse-checkout set dir1 dir2 | add dir | list | disablegit clone --depth 1 URL shallow clonegit fetch --unshallowKey takeaways
- Submodules pin another repository at an exact commit; powerful but easy to get wrong, so consider packages first.
- Git LFS keeps large binaries out of Git history, storing small pointers instead; commit
.gitattributes. - Partial clone and sparse checkout let you work on part of a huge repository quickly.
- Shallow clones speed up CI; fetch more history only when a job needs it.