Git Large File Storage (Git LFS), a tool used to manage large files and binaries in Git repositories. Standard Git wasn’t designed to handle large files, and Git LFS provides a solution by storing large files outside the repository while keeping lightweight pointers within it.
Git Large File Storage (Git LFS) is a powerful extension for Git that allows efficient tracking of large files. Instead of storing entire file versions in the Git repository, Git LFS stores only references (pointers) to large files, which are saved separately on a designated server. When cloning, Git LFS ensures only the necessary versions of the large files are retrieved, optimizing the size and speed of Git operations.
.zip
archives or .exe
filesGit LFS improves the workflow in various ways:
The first step to using Git LFS is to install it on your local machine.
Installation varies slightly based on the operating system. Here are the most common installation methods:
brew install git-lfs
sudo apt-get install git-lfs
After installation, enable Git LFS for your repository with:
git lfs install
Output: Running git lfs install
enables Git LFS hooks for the repository, which will manage large files automatically.
After installing Git LFS, it’s time to configure it to track specific types of files in your repository. This configuration is done by adding tracking rules to a .gitattributes
file, which controls which file types Git LFS will manage.
To configure Git LFS to track a certain type of file, use the git lfs track
command. For example, to track .png
images:
git lfs track "*.png"
*.png
: Specifies all .png
files, instructing Git LFS to manage these files instead of Git.After adding a file type for tracking, Git LFS automatically updates the .gitattributes
file with an entry.
*.png filter=lfs diff=lfs merge=lfs -text
Once Git LFS is configured to track specific files, commit the .gitattributes
file to the repository.
git add .gitattributes
git commit -m "Configure Git LFS to track .png files"
You can add specific files to Git LFS tracking in addition to file types.
To track a specific file (e.g., large-file.zip
):
git lfs track "large-file.zip"
git add .gitattributes
git commit -m "Track large-file.zip with Git LFS"
Git LFS tracks large files automatically once configured, allowing you to add, commit, push, pull, and clone repositories without manually managing large files.
Once a file is tracked by Git LFS, commit and push it to the repository like any other file:
git add large-image.png
git commit -m "Add large image to repository"
Output: When you check the commit history, you’ll see a pointer reference for large-image.png
rather than the full file content.
Git LFS includes advanced options, including setting a custom storage location, managing quotas, and more.
If your organization has a custom server for large file storage, configure Git LFS to use it:
git config lfs.url
Explanation: This command redirects Git LFS to use a specific server for file storage, rather than the default provider.
Git LFS offers commands to monitor and manage storage usage, helping you stay within limits:
git lfs ls-files
Output: Lists all files managed by Git LFS along with their sizes, giving you insight into storage consumption.
Below is an example workflow for integrating Git LFS into a project.
git lfs install
git lfs track "*.jpg"
git lfs track "*.mp4"
git add .gitattributes
git commit -m "Add .jpg and .mp4 files to Git LFS tracking"
git add large-video.mp4
git commit -m "Add large video file"
git push origin main
Output: This workflow demonstrates a typical setup where specific file types are tracked and managed using Git LFS, allowing large media files to be added without performance issues.
Git LFS is essential for managing large files in Git, enabling efficient storage, retrieval, and management of files that are traditionally problematic for Git. By using Git LFS, developers can track large files seamlessly, allowing for a smoother Git workflow and efficient team collaboration. Happy Coding!❤️