lfs Git Command Guide
Git LFS (Large File Storage) is a system for managing and versioning large files in association with a Git repository. Instead of storing large files within the Git repository as blobs, Git LFS stores special “pointer files” in the repository while storing the actual file contents on a Git LFS server.
Git LFS Architecture:
Section titled “Git LFS Architecture:”How Git LFS Works:
Section titled “How Git LFS Works:”- Pointer Files: Git repository stores small text files containing LFS object metadata
- Clean Filter: Converts large files to pointer files when staging
- Smudge Filter: Converts pointer files back to large files when checking out
- LFS Server: Stores actual large file content separately from Git repository
- Pre-push Hook: Uploads large file content to LFS server before Git push
Pointer File Format:
Section titled “Pointer File Format:”version https://git-lfs.github.com/spec/v1oid sha256:abc123...size 1048576Git LFS Installation and Setup:
Section titled “Git LFS Installation and Setup:”Basic Installation:
Section titled “Basic Installation:”# Install Git LFScurl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bashsudo apt-get install git-lfs
# Or on macOSbrew install git-lfs
# Initialize in repositorycd my-repogit lfs installRepository Configuration:
Section titled “Repository Configuration:”# Track specific file typesgit lfs track "*.psd"git lfs track "*.zip"git lfs track "assets/videos/*"
# Track files by size thresholdgit lfs track --filename "*.mov" --size-threshold 100MB
# View tracked patternsgit lfs trackRemote Storage Configuration:
Section titled “Remote Storage Configuration:”# Configure LFS server URLgit config lfs.url https://lfs-server.company.com/repo.git/info/lfs
# Use GitHub LFS (automatic)git remote add origin https://github.com/user/repo.git
# Use GitLab LFS (automatic)git remote add origin https://gitlab.com/user/repo.gitFile Tracking and Management:
Section titled “File Tracking and Management:”Track Large Files:
Section titled “Track Large Files:”# Track specific file typesgit lfs track "*.mp4" "*.mov" "*.zip"
# Track individual filesgit lfs track "large-dataset.bin"
# Track by patterngit lfs track "assets/**/*"View Tracking Status:
Section titled “View Tracking Status:”# List tracked patternsgit lfs track
# Show tracked files in repositorygit lfs ls-files
# Check LFS file statusgit lfs statusUntrack Files:
Section titled “Untrack Files:”# Stop tracking specific patterngit lfs untrack "*.tmp"
# Untrack specific filegit lfs untrack "obsolete-file.dat"
# Migrate away from LFSgit lfs migrate export --include="*.mov"Workflow Integration:
Section titled “Workflow Integration:”Standard Git Workflow with LFS:
Section titled “Standard Git Workflow with LFS:”# Initialize LFS in repositorygit lfs install
# Track large file typesgit lfs track "*.mov" "*.mp4" "*.zip"
# Add .gitattributes to repositorygit add .gitattributesgit commit -m "Add LFS file tracking"
# Work with large files normallygit add large-video.movgit commit -m "Add product demo video"
# Push normally (LFS handles large files)git push origin mainMigration to LFS:
Section titled “Migration to LFS:”# Migrate existing large files to LFSgit lfs migrate import --include="*.mov,*.mp4"
# Or migrate specific filesgit lfs migrate import --include-ref=HEAD --include="assets/videos/*"
# Push LFS objectsgit push origin mainLFS-Specific Operations:
Section titled “LFS-Specific Operations:”# Fetch LFS objectsgit lfs fetch origin
# Pull with LFS objectsgit lfs pull origin
# Check LFS object integritygit lfs fsckAdvanced LFS Features:
Section titled “Advanced LFS Features:”Batch Operations:
Section titled “Batch Operations:”# Process multiple LFS filesgit lfs ls-files | xargs -I {} git lfs checkout {}
# Batch migrate filesfind assets/ -name "*.mov" -exec git lfs track {} \;
# Bulk status checkgit lfs status | grep -E "(modified|untracked|ignored)"Lock and Collaboration:
Section titled “Lock and Collaboration:”# Lock file for exclusive editinggit lfs lock "critical-document.docx"
# Check lock statusgit lfs locks
# Unlock when donegit lfs unlock "critical-document.docx"
# Force unlock (admin)git lfs unlock --force "critical-document.docx"Server Management:
Section titled “Server Management:”# Prune old LFS objectsgit lfs prune
# Deduplicate LFS storagegit lfs dedup
# Check LFS server connectivitygit lfs envConfiguration and Customization:
Section titled “Configuration and Customization:”Global Configuration:
Section titled “Global Configuration:”# Set default LFS settingsgit config --global lfs.concurrenttransfers 8git config --global lfs.basictransfersonly falsegit config --global lfs.tustrictattributes false
# Configure transfer adaptersgit config --global lfs.transfer.maxretries 5git config --global lfs.transfer.maxretrydelay 10sRepository-Specific Settings:
Section titled “Repository-Specific Settings:”# Set LFS URL for this repositorygit config lfs.url https://custom-lfs-server.com/repo.git/info/lfs
# Configure batch sizegit config lfs.batchtransfer truegit config --add lfs.transfer.maxbatchsize 100Environment Variables:
Section titled “Environment Variables:”# Override LFS settingsexport GIT_LFS_SKIP_SMUDGE=1 # Skip downloading LFS objectsexport GIT_LFS_PROGRESS=1 # Show progress barsexport GIT_LFS_TRACE=1 # Enable debug tracingIntegration with CI/CD:
Section titled “Integration with CI/CD:”Automated LFS Setup:
Section titled “Automated LFS Setup:”#!/bin/bash# CI/CD pipeline LFS setup
# Install Git LFScurl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | bashapt-get install -y git-lfs
# Initialize LFSgit lfs install
# Pull LFS objectsgit lfs pull
# Verify LFS objectsgit lfs fsck
# Run tests with LFS filesnpm testBuild System Integration:
Section titled “Build System Integration:”# Makefile with LFS awarenessassets: $(LFS_FILES) @echo "LFS files ready for build"
# Check LFS status in buildcheck-lfs: @if ! git lfs status | grep -q "Git LFS objects"; then \ echo "No LFS objects found"; \ fiTroubleshooting Common Issues:
Section titled “Troubleshooting Common Issues:”LFS Not Working:
Section titled “LFS Not Working:”# Check LFS installationgit lfs version
# Verify LFS initializationgit lfs env
# Check .gitattributescat .gitattributes | grep -i lfs
# Reinitialize if neededgit lfs install --forceNetwork and Authentication:
Section titled “Network and Authentication:”# Test LFS server connectivitygit lfs env | grep url
# Check authenticationgit config lfs.url
# Debug transfer issuesGIT_LFS_TRACE=1 git lfs fetch 2>&1 | head -20File Corruption Issues:
Section titled “File Corruption Issues:”# Verify LFS object integritygit lfs fsck
# Check specific objectgit lfs fsck --objects abc123...
# Repair corrupted objectsgit lfs prune --verify-remotePerformance Issues:
Section titled “Performance Issues:”# Check transfer settingsgit config lfs.concurrenttransfers
# Optimize for large filesgit config lfs.transfer.maxretries 10git config lfs.transfer.maxretrydelay 30s
# Monitor LFS operationsgit lfs status --porcelainReal-World Usage Examples:
Section titled “Real-World Usage Examples:”Media Production Repository:
Section titled “Media Production Repository:”#!/bin/bash# Setup for video production team
# Initialize LFSgit lfs install
# Track all media filesgit lfs track "*.mov" "*.mp4" "*.wav" "*.aiff"git lfs track "*.psd" "*.ai" "*.jpg" "*.png"git lfs track "assets/**/*"
# Configure for large file handlinggit config lfs.concurrenttransfers 16git config lfs.transfer.maxbatchsize 50
# Add to repositorygit add .gitattributesgit commit -m "Configure LFS for media files"
# Team workflowecho "Media files now tracked with LFS - normal Git workflow applies"Game Development Assets:
Section titled “Game Development Assets:”# Game asset management with LFS
# Track game assetsgit lfs track "*.fbx" "*.obj" "*.dae" # 3D modelsgit lfs track "*.png" "*.jpg" "*.tga" # Texturesgit lfs track "*.wav" "*.ogg" "*.mp3" # Audiogit lfs track "*.unitypackage" # Unity packages
# Configure for game developmentgit config lfs.url https://git-lfs.company.com/game-assets.git/info/lfs
# Asset pipeline integrationgit lfs track "Assets/Textures/**/*"git lfs track "Assets/Models/**/*"git lfs track "Assets/Audio/**/*"
# Lock critical assetsgit lfs lock "Assets/Levels/main-level.unity"Scientific Data Repository:
Section titled “Scientific Data Repository:”# Large dataset management
# Track scientific data filesgit lfs track "*.hdf5" "*.nc" "*.mat" # Scientific data formatsgit lfs track "*.csv" "*.xlsx" # Data tablesgit lfs track "datasets/**/*" # Dataset directories
# Configure for large data transfersgit config lfs.concurrenttransfers 4git config lfs.transfer.maxbatchsize 10git config lfs.transfer.maxretrydelay 60s
# Data validation workflowgit lfs track "validation-scripts/*"git add .gitattributesgit commit -m "Configure LFS for scientific datasets"How does Git LFS improve repository performance?
Section titled “How does Git LFS improve repository performance?”LFS stores only pointer files in Git (typically <1KB each), dramatically reducing clone times and repository size while maintaining full file history.
What’s the difference between Git LFS and Git Annex?
Section titled “What’s the difference between Git LFS and Git Annex?”Git LFS is simpler, focused on large files with centralized storage. Git Annex is more flexible, supports distributed storage, but has steeper learning curve.
Can Git LFS work with existing repositories?
Section titled “Can Git LFS work with existing repositories?”Yes, can migrate existing large files to LFS using git lfs migrate import. Works with any Git repository, doesn’t require repository recreation.
How do LFS locks work for collaboration?
Section titled “How do LFS locks work for collaboration?”LFS locks prevent concurrent editing of binary files. When locked, other users can’t push changes to that file until lock is released.
What’s the impact of LFS on Git operations?
Section titled “What’s the impact of LFS on Git operations?”Most Git operations work normally. git clone/pull/fetch automatically handle LFS objects. Only storage location differs - LFS server vs Git repository.
Can LFS work without internet connection?
Section titled “Can LFS work without internet connection?”No, requires LFS server connectivity for downloading/uploading large file content. Pointer files work offline, but actual content requires network access.
How do I migrate from LFS back to regular Git?
Section titled “How do I migrate from LFS back to regular Git?”Use git lfs migrate export to convert LFS files back to regular Git blobs. Requires LFS server access and may create very large repository.
What’s the relationship between LFS and Git submodules?
Section titled “What’s the relationship between LFS and Git submodules?”LFS manages individual large files; submodules manage entire repositories. Can use both together - LFS for large assets, submodules for component repositories.
How do I handle LFS in CI/CD pipelines?
Section titled “How do I handle LFS in CI/CD pipelines?”Install Git LFS in pipeline, run git lfs pull to download objects, use git lfs status to verify. Configure LFS authentication for pipeline access.
Can LFS work with Git protocols other than HTTP?
Section titled “Can LFS work with Git protocols other than HTTP?”Yes, works with HTTP/HTTPS, SSH, and Git protocols. LFS server endpoint configured separately from Git remote URL.
What’s the storage overhead of LFS pointer files?
Section titled “What’s the storage overhead of LFS pointer files?”Pointer files are very small (~100-200 bytes each), containing only metadata. Actual file content stored on LFS server, not in Git repository.
How do I handle LFS objects in Git hooks?
Section titled “How do I handle LFS objects in Git hooks?”LFS objects available during hooks. Use git lfs status to check LFS file state. Pre-push hook automatically uploads new LFS objects.
Can LFS work with Git worktrees?
Section titled “Can LFS work with Git worktrees?”Yes, LFS works normally with worktrees. Each worktree can have different LFS object versions checked out as needed.
What’s the difference between LFS and Git’s large file handling?
Section titled “What’s the difference between LFS and Git’s large file handling?”Git has 2GB blob limit and stores all content in repository. LFS removes size limits and stores content externally while maintaining Git workflow.
How do I troubleshoot LFS authentication issues?
Section titled “How do I troubleshoot LFS authentication issues?”Check LFS URL configuration, verify credentials, test with curl against LFS endpoint, check for certificate issues, verify LFS server accessibility.
Can LFS work with GitHub/GitLab’s built-in LFS?
Section titled “Can LFS work with GitHub/GitLab’s built-in LFS?”Yes, GitHub and GitLab provide LFS server functionality. No additional server setup required - just enable LFS and push normally.
How do I handle LFS objects in deployment?
Section titled “How do I handle LFS objects in deployment?”Deploy scripts should run git lfs pull to ensure large files are available. Consider LFS object caching for faster deployments.
What’s the impact of LFS on repository size?
Section titled “What’s the impact of LFS on repository size?”Dramatically reduces repository size by moving large file content to LFS server. Only pointer files remain in Git repository.
Can LFS work with shallow clones?
Section titled “Can LFS work with shallow clones?”Yes, but shallow clones may not include LFS objects. Use git lfs fetch to get LFS objects for shallow repository, or avoid shallow clones with LFS.
How do I monitor LFS usage and costs?
Section titled “How do I monitor LFS usage and costs?”Use git lfs ls-files to see tracked files, git lfs status for transfer status, LFS server analytics for storage usage and costs.
Applications of Git LFS
Section titled “Applications of Git LFS”- Media Production: Manage large video, audio, and image files in creative projects
- Game Development: Handle large game assets, textures, models, and audio files
- Scientific Computing: Store large datasets, simulation results, and research data
- Enterprise Applications: Manage large binary assets, documents, and media files
- Machine Learning: Version control large model files, datasets, and training data
- Digital Asset Management: Organize and version control large media libraries and archives