Efficiently Managing Large Files in Git with LFS
Handling large files efficiently is crucial in Git-managed projects, but it’s important not to slow down operations like commits. Git Large File Storage (LFS) is a Git extension designed to solve this problem. It maintains versions of the large files it tracks, ensuring the fundamental versioning capabilities of Git are not compromised.
Unlike traditional Git, which compresses and stores all files in the repository, Git LFS stores large files on a separate server. This approach involves maintaining pointers in the repository to these large files.
Installing Git LFS
To install Git LFS on your system, visit Git LFS Download and follow the instructions.
For initial setup, run:
git lfs install
This command configures Git LFS on your system.
Tracking Large Files in Your Repository
To track large files, such as videos in MP4 format, use the command:
git lfs track "*.mp4"
This command ensures that MP4 files are handled using Git LFS. Don’t forget to track your `.gitattributes` file:
git add .gitattributes
Automating Git LFS Setup
You can automate the setup process for different repositories by using a shell script , I am using the name setuplfs.sh:
#!/bin/bash
# Define an array of file extensions
file_extensions=(
"*.png" "*.jpg" "*.jpeg" "*.gif" "*.bmp" "*.tiff" "*.psd"
"*.mp4" "*.mov" "*.wmv" "*.flv" "*.avi" "*.mkv"
"*.jar" "*.log" "*.zip" "*.rar" "*.7z" "*.exe"
"*.tscproj"
# Add other file extensions here
)
# Loop through the array and track each extension with Git LFS
for extension in "${file_extensions[@]}"; do
git lfs track "$extension"
done
echo "Git LFS tracking setup complete."
Make the script executable with `chmod +x setuplfs.sh`. Move it to a folder that’s in your global PATH. This allows you to run the script in any Git repository.
Usage:
After cloning or initializing a repository, run setuplfs.sh to configure Git LFS for commonly used file extensions. This script automatically tracks the specified file types with Git LFS, simplifying the process of setting up LFS in new repositories.