Sub topic 3
Installing Git LFS and Enabling It for Your User
This chapter covers installing Git LFS and enabling it for your user account. It assumes you can clone, add, commit, push and pull with Git, and that you know what remotes and branches are. It also assumes you can work in a terminal and know that .gitattributes holds per-path settings, as .gitignore does for ignored files. You don't need any LFS knowledge yet. The previous chapter explained that LFS stores small pointer files in Git and keeps the real content on a separate server. This chapter sets up the machinery that does that work.
Two separate steps
Git LFS needs two things on your machine:
- The
git-lfsprogram, which Git finds on yourPATHand runs asgit lfs .... - The configuration that tells Git to call that program when files are added, checked out, or pushed.
A package manager does the first step. The command git lfs install does the second. Installing the package does not always do the second step for you. Some installers run it automatically and some don't, so it is worth knowing how to check and repair it.
Installing the program
Use the method that fits your platform.
| Platform | Command or source |
|---|---|
| macOS (Homebrew) | `brew install git-lfs` |
| Debian and Ubuntu | `sudo apt-get install git-lfs` |
| Fedora and RHEL family | `sudo dnf install git-lfs` |
| Windows | Included with the Git for Windows installer, or download an installer from git-lfs.com |
On Debian and Ubuntu the packaged version can lag behind the latest release. If you need a newer version, the Git LFS project publishes its own package repositories and release archives. Check the install instructions on git-lfs.com for your distribution.
After installing, confirm that Git can find the program:
git lfs versionThe output is a single line that starts with git-lfs/ followed by a version number, then details about the build. If Git answers 'lfs' is not a git command, the git-lfs executable is not on your PATH. Open a new terminal first, since PATH changes often apply only to new sessions. If that doesn't help, check where the package manager put the binary.
Enabling LFS for your user
Run this once per user account, from any directory:
git lfs installThis command does the setup that makes LFS work. The following sections cover what it changes.
Filter settings in your global config
Git has a mechanism called a filter driver. A filter is a pair of commands attached to a name. Git runs the first command (the clean filter) on a file's content as it is staged. It runs the second (the smudge filter) on content as it is written into your working directory. Git LFS registers itself under the filter name lfs. The .gitattributes line you will meet in the next chapter, filter=lfs, selects it for matching files.
git lfs install writes the registration into your global Git config, ~/.gitconfig. You can inspect it:
git config --global --get-regexp '^filter\.lfs\.'Typical output looks 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 trueEach setting does the following:
cleanconverts a file's real content into a pointer file when you stage it. It also stores the real content in the local LFS cache.%fis replaced by the file path.smudgedoes the reverse at checkout. It replaces a pointer with the real content, fetching it from the server if it is not cached.processis a long-running variant of both. Git starts onegit-lfs filter-processinstance and streams many files through it. Without this, Git would start a new process per file, which is slow in repositories with thousands of LFS files. Git usesprocessin preference tocleanandsmudgewhen it is available.required = truemakes Git fail with an error if the filter cannot run. Without it, Git would quietly commit the full binary content, bypassing LFS.
The required setting protects you from one failure. Suppose a teammate clones a repository on a machine where git-lfs is missing. With required = true, their git add stops with an error. Without it, they could commit large binaries straight into Git history. Your remote may then reject the push, or you may end up with the very problem LFS is meant to prevent.
Hooks in the repository
A hook is a script in a repository's hooks directory (normally .git/hooks) that Git runs at a specific moment. Hooks are local to each clone and are never pushed or cloned. When you run git lfs install inside a repository, it also writes four hooks there:
| Hook | What LFS uses it for |
|---|---|
| `pre-push` | Uploads LFS objects to the LFS server before Git pushes commits. |
| `post-checkout` | Updates file permissions for lockable files after a checkout. |
| `post-commit` | Updates file permissions for lockable files after a commit. |
| `post-merge` | Updates file permissions for lockable files after a merge. |
The pre-push hook matters most. When you push, Git calls the hook first. The hook finds the pointer files in the commits being pushed and uploads their real content to the LFS server. Only after that does Git send the commits, which contain only the pointers. If the hook is missing, Git pushes pointers whose content never reached the server. Teammates who fetch those commits then get pointers they cannot resolve. This is a common cause of the "missing object" errors covered in the troubleshooting chapter.
Scopes and options
By default git lfs install modifies your global config. You can choose a different scope or change what is installed.
| Command | Effect |
|---|---|
| `git lfs install` | Global filter settings in `~/.gitconfig`. Also installs hooks if run inside a repository. |
| `git lfs install --skip-repo` | Global filter settings only, even when run inside a repository. |
| `git lfs install --local` | Writes the filter settings to the current repository's `.git/config` instead of the global file. |
| `git lfs install --system` | Writes to the system-wide Git config (often `/etc/gitconfig`). Needs administrator rights. |
| `git lfs install --skip-smudge` | Sets the smudge filter so that checkouts leave pointer files in place instead of downloading content. |
| `git lfs install --force` | Overwrites existing LFS filter settings and hooks. |
Some practical notes:
- Per-user is the usual choice. It fits a laptop you use for many projects. The system scope suits shared build machines where every account should have LFS.
--localis useful when you want LFS only in one repository, or when your global config is managed by another tool.- Existing hooks can block installation. If a repository already has a
pre-pushhook of its own,git lfs installrefuses to overwrite it and prints an error. Either merge the LFS call into your hook by hand (the LFS hook's body runsgit lfs pre-push "$@") or use--forceto replace it. Replacing it discards your existing hook, so copy it first. core.hooksPathchanges where hooks go. If you have set this option, for example to share hooks across repositories, LFS installs its hooks into that directory.--skip-smudgeis a tool for special cases, such as partial downloads, which a later chapter covers. If you set it globally and forget, every clone will have pointer files instead of content.
To turn LFS off for your user, run git lfs uninstall. It removes the filter settings and, inside a repository, the hooks. Files already stored as pointers stay pointers, so uninstalling does not convert history back to ordinary files.
Verifying the setup
The command git lfs env prints the environment LFS sees: versions, endpoints, storage paths, and the filter configuration.
git lfs envRead the output in this order:
- The first lines show the
git-lfsand Git versions. Endpoint=shows the LFS server URL for the current repository. Outside a repository this part is empty.LocalMediaDir=andLocalReferencesDir=show where the repository keeps LFS objects, under.git/lfs.- The last lines, starting with
git config filter.lfs., show the filter settings. If these are missing,git lfs installhas not run for your user.
A quick end-to-end check of the whole setup is to run these commands in a fresh test repository:
git init lfs-smoke-test
cd lfs-smoke-test
git lfs install
ls .git/hooks/pre-push .git/hooks/post-checkout
git config --get filter.lfs.requiredIf the two hook files are listed and the last command prints true, both the user-level and repository-level pieces are in place. You can delete the test directory afterwards.
Common installation problems
git: 'lfs' is not a git command. The binary is not onPATH. Open a new terminal, or reinstall.- Filter settings missing after a package install. Some packages do not run
git lfs install. Run it yourself. - Hooks missing in one repository. Run
git lfs installinside that repository. - A hook conflict error. An existing
pre-pushhook is in the way. Merge by hand or use--forceafter saving a copy. - Two Git installations. On Windows and macOS you may have one Git from an installer and another from a package manager. Each reads the same
~/.gitconfig, butgit-lfsmust be reachable from the one you actually run. Usewhich git(orwhere giton Windows) to see which one you are using.
Exercise
A teammate says that after they pushed a commit containing a tracked .psd file, everyone else who pulled it received a small text file instead of the image. On their machine, git config --global --get-regexp '^filter\.lfs\.' prints the correct four settings, but ls .git/hooks/pre-push in the repository reports that the file does not exist. What most likely went wrong, and what single command in that repository fixes the setup for future pushes?
Show answer
The pre-push hook is missing, so Git pushed the commits containing pointer files without first uploading the real file content to the LFS server. The user-level filter settings were fine, so the pointers were created correctly. The hook is what uploads content. Running git lfs install inside the repository installs the hooks. The already-pushed commit still needs its content uploaded, for example with git lfs push --all origin or git lfs push --object-id for specific objects, so that other people can fetch it.