Sub topic 8
Moving Existing Files into LFS with git lfs migrate
This chapter assumes you can commit, push, and pull with Git, you understand branches and remotes, and you know what .gitattributes does. It also assumes you have already installed Git LFS and tracked patterns with git lfs track.
Why tracking is not enough
git lfs track only affects files you add after the pattern exists. If a .psd file was committed before you set up LFS, its full content is still stored as a regular Git object in history. Every clone still downloads it, and the repository stays heavy.
To fix this, you rewrite history so the old commits contain pointer files instead of the real content. The command for that is git lfs migrate import.
What the command does
git lfs migrate import walks the commits you select. For each file that matches --include, it replaces the blob with a pointer file and stores the real content in the local LFS object store. It also updates .gitattributes so the pattern is tracked.
Because each commit's contents change, each commit gets a new ID. Every descendant commit changes too. This is the same kind of rewrite you get from an interactive rebase.
Running the migration
Work in a fresh clone or make a backup first. A rewrite is hard to undo once pushed.
git lfs migrate import --include="*.psd" --everything
git push --force-with-lease --allThe flags mean the following:
--include="*.psd"selects the files to convert. You can list several patterns separated by commas.--everythingrewrites all local and remote-tracking branches and tags. Without it, only the current branch is rewritten.--force-with-leaseoverwrites the remote branch only if it still matches what you last fetched. This is safer than a plain--force.
If you use --everything, the push sends the new LFS objects to the LFS server and the rewritten commits to the Git remote. Tags are not covered by --all, so push them as well with git push --force-with-lease --tags if you rewrote them.
Finding out what to migrate
Before you rewrite anything, check which file types take up the most space:
git lfs migrate info --everythingmigrate info is read-only. It lists the extensions that use the most space across history, so you can choose sensible --include patterns. By default it only shows the top entries above a size threshold. You can adjust this with --above=1MB and --top=10.
Coordinating with your team
A history rewrite affects everyone who has cloned the repository. Agree on a time and follow these steps:
- Ask teammates to push or save any work in progress.
- Run the migration and force push.
- Have each teammate re-clone, or reset their branches to the new remote history.
- Close or rebase open pull requests, because their base commits no longer exist.
Teammates who merge old branches into the new history will bring the large blobs back. They will also create duplicate commits.
Common problems
| Symptom | Likely cause |
|---|---|
| Working tree is dirty and the command refuses to run | Uncommitted changes. Commit or stash them first. |
| Files still appear in old tags | You did not use `--everything`, or you forgot to push tags. |
| Repository size on the server did not shrink | The server still holds the old objects until it runs garbage collection. |
| Teammates see the old files again | They merged or pushed branches based on the old history. |
Note that LFS storage on the server counts the migrated files against your quota, while the old Git blobs may stay until the host cleans them up.
Exercise
You ran git lfs migrate import --include="*.psd" without --everything, and then force pushed only main. What problem might remain, and how would you fix it?
Show answer
Only the current branch was rewritten. Other branches and tags still contain the full .psd blobs in their history, and their commit IDs differ from the rewritten ones. Re-run the migration with --everything (ideally from a fresh clone), then force push all branches with git push --force-with-lease --all and push tags separately.
Exercise
Which read-only command helps you decide which patterns to pass to --include?
Show answer
git lfs migrate info --everything. It lists file extensions by the space they use in history without changing anything.