Getting Started with Git Large File Storage (LFS)

July 17, 2024

  • Tutorial
Thumbnail

Getting Started with Git Large File Storage (LFS)

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:

Installing Git LFS

Step 1: Install git-lfs using Homebrew

To begin, install Git LFS via Homebrew:

brew install git-lfs

Step 2: Install git-lfs Command Line Extension

Run this command once per user account to install the Git LFS command line extension:

git lfs install

Step 3: Track File Types in Your Repository

In each Git repository where you want to use Git LFS, run the following command to manage the file types:

git lfs track "*.psd"

Step 4: Track the .gitattributes File

Add 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.

Step 5: Commit and Push to GitHub

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

Migrating Existing Files

If you have existing files in your repository that you want to convert to Git LFS, follow these steps:

Step 1: Identify Top File Types

Run this command to find out the top 100 file types in your repository across all branches:

git lfs migrate info --everything --top=100

Step 2: Commit or Stash Non-Committed Files

Ensure all files are committed or stashed to prevent data loss during the Git history rewrite.

Step 3: Convert Files to Git LFS

Use this command to convert files to Git LFS:

git lfs migrate import --everything --include="*.mp3,*.psd"

Step 4: Force Push to Remote

Force push the changes to your remote repository:

git push --force --all

Step 5: Validate Migration

Verify that your files have been correctly migrated:

git lfs ls-files

Step 6: Repopulate Your Local Repository

Repopulate your local repository with file objects instead of pointers:

git lfs checkout

Cloud Deployment Configuration

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.