Sub topic 8
Moving existing files into LFS with git lfs migrate
This chapter assumes you can already track patterns with git lfs track, commit and push LFS files, and use git rebase and git push --force-with-lease on a branch. It also assumes you know what a commit ID is and why changing one affects everyone who shares the repository.
Why tracking is not enough
Running git lfs track "*.psd" only affects files you add from now on. If the repository already contains .psd files, every old commit still holds the full binaries. Clones stay large, and the history keeps the weight you wanted to remove.
Making old commits use pointer files means changing them. A commit ID is a hash of its contents, so any change to a file changes the ID of that commit and of every commit after it. git lfs migrate import does this rewrite for you. It walks the history, replaces matching file contents with pointer files, uploads nothing yet, and stores the real content in your local LFS object store.
Step 1: Find what is worth migrating
Before you rewrite anything, measure. git lfs migrate info reads history without changing it and reports which file types take the most space.
git lfs migrate info --everything
git lfs migrate info --everything --above=5mb --top=10
git lfs migrate info --include-ref=refs/heads/main --include="*.psd,*.zip"The first command lists the top file extensions by total size. By default it considers files above 1 MB and shows the top 5 entries. --above sets the size threshold and --top sets how many rows you see. --everything examines all local and remote branches and tags instead of only the current branch.
The output groups entries by extension, so it is a good way to choose patterns. Pick patterns that match a file type, not individual paths. A broad pattern such as *.zip is easier to maintain than a list of specific files.
Step 2: Prepare and back up
A history rewrite is hard to undo once collaborators have pulled the new commits. Prepare before you run it.
- Make sure the working tree is clean.
git statusshould show nothing to commit. Migrate refuses to run with uncommitted changes. - Fetch everything so your local refs match the remote:
git fetch --all --tags. - Make a backup. A mirror clone keeps every ref and is the safest option.
- Tell the team a rewrite is coming, and agree on a time when nobody pushes.
git fetch --all --tags
git clone --mirror . ../assets-backup.gitIf the repository has open pull requests, the rewrite will orphan them. The pull request commits no longer match the rewritten branch. Merge or close them first, or plan to recreate them.
Step 3: Run the migration
The core command names the patterns to convert and the refs to rewrite.
git lfs migrate import --include="*.psd" --everything
git push --force-with-lease --allWhat each part does:
importis the direction. It converts regular Git blobs to LFS pointers.--include="*.psd"is a comma-separated list of patterns. Only matching files change.--everythingrewrites every local branch and tag, and also the remote-tracking branches.git lfs migrate importalso updates.gitattributesin each rewritten commit, so the tracking rule exists wherever the pointer files do. You do not need to rungit lfs trackfirst.
Several patterns can be given in one run, and you can exclude paths.
git lfs migrate import --everything --include="*.psd,*.zip,assets/**" --exclude="assets/readme.txt"Limiting which refs change
--everything is the most complete choice, but you can narrow the scope.
| Option | Effect |
|---|---|
| (none) | Rewrites only the current branch. |
| `--include-ref=refs/heads/main` | Rewrites the named ref, and can be repeated. |
| `--exclude-ref=refs/heads/legacy` | Leaves the named ref untouched. |
| `--everything` | Rewrites all local and remote branches and all tags. |
Rewriting only some branches can leave your repository in an inconsistent state. A branch that you skipped still contains the old blobs, and its history now diverges from the rewritten one. Unless you have a reason, rewrite everything.
Pushing the result
Because every commit ID changed, a normal git push is rejected as non-fast-forward. Use a force push, and prefer --force-with-lease. It refuses to overwrite the remote if someone pushed after your last fetch.
git push --force-with-lease --all
git push --force-with-lease --tags--all pushes branches only. Tags need their own push. The LFS content uploads as part of the push through the pre-push hook. If the server rejects the force push, a branch protection rule is the usual cause. Lift it temporarily, push, and restore it.
Step 4: Verify
Check that the migration did what you expected before telling the team it is done.
git lfs ls-files --all | head
git lfs migrate info --everything
git count-objects -vHgit lfs ls-files --all lists LFS files across all of history, not only the current checkout. A second migrate info should show no large .psd blobs left in regular Git storage. git count-objects -vH shows the local repository size.
The local size may not shrink at once. The old objects remain reachable through the reflog and through refs such as refs/original in some tools. To reclaim space in your local copy, expire the reflog and collect garbage:
git reflog expire --expire-unreachable=now --all
git gc --prune=nowOnly do this after you are sure the new history is correct and your backup exists.
What collaborators must do
Everyone else has the old history. If they run git pull, Git tries to merge two histories that share no recent commits, and they will see duplicated or conflicting commits. Give them one of these two instructions:
- Re-clone (simplest and safest). Delete the old clone and run
git cloneagain. Any unpushed work must be saved first, for example as a patch. - Reset in place. Fetch, then reset each branch to the remote one:
git fetch originfollowed bygit reset --hard origin/main. This discards local commits on that branch, so they should first move their work to a new branch and rebase it.
Work that was based on old commits can be moved with git rebase --onto, but the old base must be named by its old commit ID. Ask people to record those IDs before the rewrite.
The server still stores old objects. Hosting providers often keep unreferenced objects until garbage collection runs, and old pull request refs can keep them alive. If you migrated to remove a leaked secret or to shrink the server repository, the hosting provider's support or maintenance tools may be needed. Migrating alone does not guarantee deletion on the server.
Migrating without rewriting history
Sometimes a rewrite is not acceptable, for example on a shared repository with many forks. For this case --no-rewrite converts only the files you name in a single new commit on top of the current branch.
git lfs migrate import --no-rewrite "design/logo.psd" "design/banner.psd"
git push origin mainThis approach changes no existing commit IDs, so a normal push works. The trade-off is that the old blobs stay in history. New clones still download them, so the repository does not get smaller. It only makes future versions of these files use LFS. It also takes file paths, not --include patterns, and it does not rewrite .gitattributes in older commits.
Going the other way
git lfs migrate export converts LFS pointers back into regular Git blobs, which is a rewrite too. You might do this when leaving LFS or moving to a host without LFS support.
git lfs migrate export --include="*.psd" --everythingCommon problems
- "Working copy must not be dirty." Commit or stash your changes first.
- A file is still a regular blob after migration. Its path did not match the pattern, or its ref was not included. Run
git lfs migrate info --everythingand compare the paths. Remember that*.psdis case-sensitive on most systems, soLOGO.PSDneeds its own pattern. - Push rejected. The rewrite changed commit IDs, so you need
--force-with-lease. If it still fails, check branch protection. - Collaborators see strange merges. They pulled instead of re-cloning. Have them reset or re-clone.
- Existing pointer files. If some commits already contain LFS pointers, the
--pointersoption controls how migrate treats them:follow(default for import),no-follow, orignore.
Exercise
Your team's repository has 300 MB of .zip and .psd files in history on all branches and tags. You want to move them to LFS and shrink new clones. Write the commands to measure, migrate, and publish the result, and explain why a plain git push fails afterward.
Show answer
Measure with git lfs migrate info --everything --above=1mb. Migrate with git lfs migrate import --include="*.zip,*.psd" --everything. Publish with git push --force-with-lease --all and git push --force-with-lease --tags. A plain push fails because the rewrite changed the commit IDs, so the local branches no longer descend from the remote ones and the server rejects the update as non-fast-forward.
Exercise
A hosting policy forbids force pushes to main. You still want future versions of design/logo.psd to use LFS. Which command do you use, and what does it not fix?
Show answer
Use git lfs migrate import --no-rewrite design/logo.psd and then a normal git push origin main. It adds one new commit with the file as an LFS pointer and changes no old commit IDs. It does not remove the old large blobs from history, so clones stay as large as before.