Git LFS: Tracking Large Media Files in WordPress Theme Repositories

Git LFS for large files in WordPress theme repositories

A theme repository with design source files, demo videos and font packages grows by hundreds of megabytes with every revision of a binary file, because git keeps every version in full. Cloning takes minutes, and the deploy server downloads files it never needs. Git LFS (Large File Storage) stores such files outside the normal git objects: the repository contains small pointer files, and the real content is downloaded only for the checked-out version.

Problem: A theme repository contains .psd and .fig exports, demo MP4 videos and a ZIP of the demo content. After two years it is 2.3 GB, and the CI checkout of every build takes three minutes.

Solution: Install Git LFS, track the binary types with git lfs track (committed in .gitattributes), migrate the existing history with git lfs migrate import if the repository must shrink, and skip LFS downloads on servers and CI jobs that do not need the files.

Track file types

git lfs install                    # once per machine: sets up the git filters
git lfs track "*.psd" "*.fig" "*.mp4" "design/**"
git add .gitattributes
git commit -m "chore: track design files and videos with Git LFS"
# .gitattributes (written by git lfs track)
*.psd filter=lfs diff=lfs merge=lfs -text
*.fig filter=lfs diff=lfs merge=lfs -text
*.mp4 filter=lfs diff=lfs merge=lfs -text
design/** filter=lfs diff=lfs merge=lfs -text

From now on, these files are stored as LFS objects when they are added. In the repository itself, each is a text pointer of about 130 bytes with the file’s hash and size. git lfs ls-files lists the tracked files of the current commit.

Shrink the existing history

Tracking new files does not remove the old versions from history. To move them:

git lfs migrate info --everything --above=1MB            # what would be migrated
git lfs migrate import --everything --include="*.psd,*.fig,*.mp4,design/**"
git push --force --all && git push --force --tags

migrate import rewrites every commit, so all commit hashes change. Everyone must re-clone afterwards, open pull requests must be rebased, and tags point to new commits. Coordinate it like any history rewrite; for a repository that is shared widely, migrating only from now on (no rewrite) is often the pragmatic choice.

Servers and CI without LFS downloads

A production server does not need the design files:

GIT_LFS_SKIP_SMUDGE=1 git clone git@github.com:agency/theme.git
# GitHub Actions: actions/checkout downloads LFS objects only with lfs: true
- uses: actions/checkout@v4
  with:
    lfs: false

With GIT_LFS_SKIP_SMUDGE=1, the working tree contains the pointer files instead of the content. If a build needs some LFS files, fetch just those: git lfs pull --include="assets/video/*.mp4". Better still, keep files that the theme serves on the website out of LFS-only paths, or out of the repository: production media belongs in uploads or a CDN, not in the theme.

What belongs in LFS and what does not

  • In LFS: design sources, videos, large binary assets that the team needs versioned.
  • Not in LFS, not in git: WordPress uploads, database dumps, built ZIPs (build them in CI from the tagged source: git tags and releases).
  • Small images that the theme uses (icons, a logo): fine in normal git, LFS adds overhead for no gain.

NOTE: LFS storage on GitHub, GitLab and Bitbucket is metered separately from repository size; check the plan’s quota before migrating gigabytes. For a large repository in general, partial clones may be the lighter fix: partial clones and sparse checkout. Housekeeping of the normal git objects: git maintenance.