0
0
GitHow-ToBeginner · 3 min read

How to Use Git LFS: Simple Guide for Large File Storage

Use git lfs install to set up Git LFS in your repository, then track large files with git lfs track <file-pattern>. Commit and push as usual; Git LFS stores large files efficiently outside the main Git history.
📐

Syntax

Git LFS commands explained:

  • git lfs install: Sets up Git LFS hooks in your repository.
  • git lfs track <file-pattern>: Tells Git LFS which files to manage (e.g., *.psd for Photoshop files).
  • git add .gitattributes: Adds the tracking rules to your repo.
  • git add <files>, git commit, git push: Work as usual; large files are handled by Git LFS.
bash
git lfs install
git lfs track "*.psd"
git add .gitattributes
git add <large-files>
git commit -m "Add large files with Git LFS"
git push
💻

Example

This example shows how to set up Git LFS, track PNG images, and push them to a remote repository.

bash
git lfs install

git lfs track "*.png"
git add .gitattributes

git add image1.png image2.png
git commit -m "Add PNG images with Git LFS"
git push
Output
Git LFS initialized. Tracking "*.png" [main abc1234] Add PNG images with Git LFS 2 files changed, 0 insertions(+), 0 deletions(-) Git LFS objects uploaded successfully.
⚠️

Common Pitfalls

Common mistakes when using Git LFS:

  • Not running git lfs install before tracking files causes hooks not to work.
  • Forgetting to add and commit the .gitattributes file means tracking rules are not saved.
  • Trying to track files after they are already committed without rewriting history will not move them to LFS.
  • Using git add before git lfs track means files are added without LFS management.
bash
git add largefile.zip
# Wrong: added before tracking

git lfs track "largefile.zip"
git add .gitattributes
# Right: track first, then add

git add largefile.zip
git commit -m "Track largefile.zip with Git LFS"
📊

Quick Reference

CommandPurpose
git lfs installInitialize Git LFS in your repo
git lfs track ""Set file types to be managed by LFS
git add .gitattributesAdd tracking rules to repo
git add Add files to staging area
git commit -m "message"Commit changes
git pushPush commits and LFS files to remote

Key Takeaways

Always run git lfs install once per repository to enable Git LFS hooks.
Use git lfs track before adding large files to ensure they are managed by LFS.
Commit the .gitattributes file to save tracking rules in your repo.
Git LFS stores large files outside normal Git history, keeping repos fast and small.
You cannot retroactively move already committed large files to LFS without rewriting history.