Sub topic 3
Installing Git LFS and enabling it for your user
This chapter assumes you can clone, commit, and push with Git, and that you know what .gitattributes does. You also need a terminal and permission to install software on your machine.
Two separate steps
Git LFS needs two things on each machine:
- The
git-lfsprogram, installed like any other tool. - A one-time per-user setup that registers LFS with Git.
Installing the program alone does not turn LFS on. Git must also know to run LFS during checkout and commit. That registration is what git lfs install does.
Installing the program
Use your system's package manager.
| Platform | Command |
|---|---|
| macOS (Homebrew) | `brew install git-lfs` |
| Debian or Ubuntu | `sudo apt install git-lfs` |
| Fedora | `sudo dnf install git-lfs` |
| Windows | Included in Git for Windows. Otherwise `winget install GitHub.GitLFS` |
Confirm that Git can find it:
git lfs versionIf you see git: 'lfs' is not a git command, the program is not installed or its folder is not on your PATH. Open a new terminal after installing, since an open shell may not see the new PATH.
Enabling LFS for your user
Run this once per user account:
git lfs installOutside a repository, the command writes filter settings to your global Git configuration (~/.gitconfig). Check them:
git config --global --get-regexp '^filter\.lfs'You should see output like this:
filter.lfs.clean git-lfs clean -- %f
filter.lfs.smudge git-lfs smudge -- %f
filter.lfs.process git-lfs filter-process
filter.lfs.required trueThese entries connect to the line that git lfs track later writes into .gitattributes (filter=lfs). Each setting has a job:
- clean runs when Git stores a file. It swaps the real content for a small pointer file and sends the content to the LFS store.
- smudge runs when Git writes a file into your working tree. It swaps the pointer back for the real content.
- process is a faster, long-running version of clean and smudge that handles many files in one process.
- required makes Git fail if LFS is missing, instead of silently committing the raw file.
The required setting protects you. Without it, a machine lacking LFS could commit a large binary straight into history.
Hooks
Inside a repository, git lfs install also installs Git hooks in .git/hooks: pre-push, post-checkout, post-commit, and post-merge. The pre-push hook is the important one. It uploads LFS content to the server before Git sends the commits that reference it.
flowchart TDInstead of a diagram, use this order of events when you push:
- Git runs the
pre-pushhook. - LFS uploads the real files to the LFS server.
- Git pushes the commits, which contain only pointer files.
Scope options
By default the setup applies to your user. You can narrow it:
git lfs install --localThe --local flag writes the filter settings to the current repository's .git/config only. This suits a shared or locked-down machine where you do not want to change global configuration. To remove the setup, run git lfs uninstall. Files already tracked stay as they are, but new checkouts will show pointer files.
Common mistakes
- Cloning before enabling LFS. The working tree then contains pointer files instead of real content. Run
git lfs install, thengit lfs pullinside the repository. - Assuming a teammate's setup covers you. Settings live in your own
~/.gitconfig, not in the repository. Every person and every build machine needs the program and the setup. - Expecting
git lfs installto track files. It only prepares Git. Choosing which files go through LFS is the next chapter's topic.
Exercise
You installed the git-lfs package on a new laptop and cloned a repository that uses LFS. The large files in the working tree are only a few lines of text starting with "version https://git-lfs.github.com/spec/v1". What step did you most likely skip, and how do you recover without recloning?
Show answer
You skipped the one-time git lfs install, so Git never ran the smudge filter that replaces pointers with real content. Run git lfs install, then git lfs pull inside the repository to download the content and update the working tree.