How to Migrate to Git LFS: Step-by-Step Guide
To migrate to
git lfs, first install Git LFS and initialize it in your repo with git lfs install. Then track large file types using git lfs track, migrate existing files with git lfs migrate import --include=<file-pattern>, and finally commit and push the changes.Syntax
Here are the main commands to migrate to Git LFS:
git lfs install: Sets up Git LFS hooks in your repository.git lfs track <file-pattern>: Tells Git LFS which file types to manage.git lfs migrate import --include=<file-pattern>: Converts existing large files in history to Git LFS.git add .gitattributesandgit commit -m "Track large files with Git LFS": Save tracking rules.
bash
git lfs install git lfs track "*.psd" git add .gitattributes git commit -m "Track large files with Git LFS" git lfs migrate import --include="*.psd"
Example
This example shows migrating all .psd files in your repo to Git LFS:
bash
git lfs install git lfs track "*.psd" git add .gitattributes git commit -m "Track PSD files with Git LFS" git lfs migrate import --include="*.psd" git push origin main
Output
Git LFS initialized.
Tracking "*.psd" files.
Committed .gitattributes.
Migrating existing *.psd files to Git LFS.
Pushed changes to remote repository.
Common Pitfalls
Common mistakes when migrating to Git LFS include:
- Not running
git lfs installbefore tracking files, so hooks are missing. - Forgetting to commit the
.gitattributesfile that stores tracking rules. - Not migrating existing large files, so old versions remain in Git history.
- Using
git lfs trackafter files are already committed without migration, which won't convert old files.
bash
Wrong: git lfs track "*.mp4" git add .gitattributes git commit -m "Track videos" # Files already committed, no migration done Right: git lfs install git lfs track "*.mp4" git add .gitattributes git commit -m "Track videos" git lfs migrate import --include="*.mp4"
Quick Reference
Summary tips for migrating to Git LFS:
- Always run
git lfs installonce per repo. - Use
git lfs trackto specify large file types. - Commit the
.gitattributesfile to save tracking rules. - Run
git lfs migrate importto convert existing files in history. - Push changes to remote after migration.
Key Takeaways
Run 'git lfs install' to set up Git LFS hooks before tracking files.
Use 'git lfs track' to specify which large files Git LFS should manage.
Commit the '.gitattributes' file to save your tracking configuration.
Migrate existing large files with 'git lfs migrate import' to rewrite history.
Push your changes after migration to update the remote repository.