Partial clones and sparse checkout for large WordPress repositories
A repository that holds a whole WordPress site, years of history and some committed media can reach several gigabytes. Cloning it for a one-line fix in one plugin downloads all of that. Git has two features to work with only part of a repository: a partial clone downloads file contents only when they are needed, and sparse checkout puts only selected folders into the working tree. Together they make a 5 GB repository feel like a small one.
Problem: An agency’s monorepo with 30 client themes and plugins has 4.8 GB of history. A clone takes 15 minutes, CI jobs spend most of their time checking out, and a developer who works on one plugin has 60,000 files in the editor.
Solution: Clone with --filter=blob:none so that only commits and trees are downloaded up front, enable git sparse-checkout in cone mode for the folders you work on, turn on the sparse index, and in CI use a shallow, blobless checkout of just the paths the job needs.
A blobless clone
git clone --filter=blob:none git@github.com:agency/sites.git
The clone contains all commits and directory trees, but no file contents (blobs) except those needed for the checked-out branch. When you check out an old commit, run git blame or git log -p on a file, git fetches the missing contents on demand. The history stays complete; only the download is lazy. The server must support partial clone, which GitHub, GitLab and Bitbucket do.
--filter=tree:0 (treeless) goes further and is good for CI jobs that only build the current commit, but slow for everyday work because almost every history command has to fetch.
Sparse checkout in cone mode
cd sites git sparse-checkout init --cone git sparse-checkout set plugins/ha-booking packages/ha-common git sparse-checkout list
Only the root files and the listed folders are in the working tree; the rest of the repository exists in git but not on disk. Commits, branches, merges and pushes work as usual. Cone mode matches whole directories, which is fast; the older pattern mode with arbitrary globs is deprecated for performance reasons.
Add a folder later with git sparse-checkout add themes/client-x, and go back to the full tree with git sparse-checkout disable.
The sparse index
git sparse-checkout init --cone --sparse-index
Without it, git’s index still lists every file of the repository, so git status and git add stay slow in a huge repository even when only one folder is checked out. The sparse index (Git 2.37+) collapses the folders outside the sparse cone into single entries, and these commands become as fast as in a small repository.
In CI
# GitHub Actions
- uses: actions/checkout@v4
with:
fetch-depth: 1
filter: blob:none
sparse-checkout: |
plugins/ha-booking
packages/ha-common
sparse-checkout-cone-mode: true
The job gets only the latest commit and only the files of the plugin and its shared library. A check-out that took two minutes now takes seconds. Monorepo structure for plugins: a monorepo for several WordPress plugins.
Large binary files
If the size comes from committed images, videos and ZIPs, a partial clone only postpones the download. Move them out of the history with git filter-repo (a rewrite of history that every developer has to re-clone after), and keep future binaries in Git LFS or, for WordPress uploads, outside git entirely.
NOTE: Tools that read the working tree directly (a PHP linter on all files, an IDE index) only see the sparse folders, which is usually what you want. Scripts that rely on files outside the cone must add them to the sparse set first. Partial clone and sparse checkout are local settings and do not affect other clones of the repository.