Managing large files in Git repositories can be a hassle, but Git Large File Storage (LFS) makes it easier. Git LFS replaces large files such as audio samples, videos, datasets, and graphics with text pointers inside Git, while storing the file contents on a remote server like GitHub.com or GitHub Enterprise. Here's how to get started with Git LFS:
git-lfs
using HomebrewTo begin, install Git LFS via Homebrew:
brew install git-lfs
git-lfs
Command Line ExtensionRun this command once per user account to install the Git LFS command line extension:
git lfs install
In each Git repository where you want to use Git LFS, run the following command to manage the file types:
git lfs track "*.psd"
.gitattributes
FileAdd the .gitattributes
file to your repository to ensure Git LFS tracks your specified file types:
git add .gitattributes
Note: Defining the file types that Git LFS should track will not automatically convert any pre-existing files to Git LFS, such as files on other branches or in your prior commit history. To do that, use the git lfs migrate command, which offers various options for different use cases.
Finally, commit and push your changes to GitHub as you normally would. For instance, if your current branch is named main
:
git add file.psd
git commit -m "Add design file"
git push origin main
If you have existing files in your repository that you want to convert to Git LFS, follow these steps:
Run this command to find out the top 100 file types in your repository across all branches:
git lfs migrate info --everything --top=100
Ensure all files are committed or stashed to prevent data loss during the Git history rewrite.
Use this command to convert files to Git LFS:
git lfs migrate import --everything --include="*.mp3,*.psd"
Force push the changes to your remote repository:
git push --force --all
Verify that your files have been correctly migrated:
git lfs ls-files
Repopulate your local repository with file objects instead of pointers:
git lfs checkout
If you're using cloud hosting and deployment services like Vercel, you may need to enable Git LFS in your project's settings. You can learn more about this process here.
By following these steps, you can effectively manage large files in your Git repositories with Git LFS, ensuring smoother workflows and better repository performance.