- Slowness in git clone and fetch operations
- Sluggish commits and status checks
- Repository size bloat
- Complexity in managing multiple branches
Git LFS installation
apt install -y git-lfsGit LFS (Large File Storage) is used to handle large files in a Git repository efficiently by replacing them with lightweight pointers while storing the actual files in a separate location. Here are various examples of using Git LFS, including tracking, untracking, checking status, and more: ---
1. Initialize Git LFS
Before using Git LFS in a repository, initialize it:git lfs installThis sets up Git LFS for the repository. ---
2. Track Large Files
To track specific file types, use:git lfs track "*.psd"or track a specific file:
git lfs track "bigfile.zip"This updates the `.gitattributes` file to include:
***.psd filter=lfs diff=lfs merge=lfs -textAfter tracking, commit the `.gitattributes` file:
git add .gitattributes git commit -m "Track large files with Git LFS"---
3. Check Tracked Files
To see which files are being tracked by Git LFS:git lfs track---
4. Check LFS Status
To check which large files are modified or committed:git lfs status---
5. Untrack a File
If you no longer want a file to be tracked by Git LFS:git lfs untrack "bigfile.zip"This removes it from `.gitattributes`. Then commit the change:
git add .gitattributes git commit -m "Untrack bigfile.zip from Git LFS"Important: This does not remove files from previous commits. ---
6. List LFS Objects
To see which LFS files exist in your repository:git lfs ls-files---
7. Migrate Large Files (if added before tracking)
If you accidentally committed a large file before tracking it with Git LFS, migrate it:git lfs migrate import --include="*.zip"---
8. Push and Fetch LFS Files
After committing, push LFS files to the remote:git push origin mainTo pull and fetch LFS files:
git pull git lfs fetch---
9. Removing LFS Files From History (if needed)
If a large file was added before tracking and you want to remove it:git lfs migrate import --include="bigfile.zip" --everythingThen, force push the cleaned history:
git push origin --force --all---
10. Verify LFS Files in Remote Repository
To check which LFS files exist on the remote:git lfs ls-files --long
Common mistakes working with Git LFS
- Not Tracking Files Properly: Forgetting to use git lfs track for large files can lead to them being stored in the repository instead of being managed by Git LFS.
- Ignoring the .gitattributes File: The .gitattributes file is crucial for Git LFS to function correctly. Failing to commit this file can cause issues for collaborators.
- Pushing Without Installing Git LFS: If Git LFS isn't installed on your system, pushing large files will fail or result in errors.
- Exceeding Storage Limits: Platforms like GitHub have storage limits for Git LFS. Exceeding these limits can block further uploads.
- Cloning Without Git LFS: If you clone a repository without Git LFS installed, you might end up with pointer files instead of the actual large files.
- Using Git LFS for Small Files: Git LFS is designed for large files. Using it for small files can unnecessarily complicate your workflow.
- Not Cleaning Up Old Files: Over time, unused large files can accumulate in the LFS storage, increasing costs or storage usage.