Git Large File Storage LFS
Git LFS is an extension for Git that allows you to efficiently manage large files and binary files in a Git repository.
Install Git LFS
1 |
sudo apt-get install git-lfs |
Ensure Git LFS is installed, you can check by running:
1 |
git lfs |
Initialize Git LFS in Your Repository
After installing Git LFS, navigate to your Git repository and initialize Git LFS:
1 |
git lfs install |
This command needs to be run once per repository to set up the necessary hooks.
Track Large Files with Git LFS
For example, to track all .psd
files, you would use:
1 |
git lfs track "*.psd" |
or add a path:
1 |
git lfs track "designs/*" |
To track files of a specific type within a directory and its subdirectories (a nested directory structure), use the ** wildcard. For instance, to track all .bin files in assets/ and any of its subdirectories, you would use:
1 |
git lfs track "assets/**/*.bin" |
The command modifies the .gitattributes
file, telling Git to use LFS for these files.
Add and Commit Files as Usual
After you’ve set up tracking, you can add and commit files as you normally would:
1 2 |
git add . git commit -m "Add large files" |
Git LFS automatically replaces large files with pointers in the Git repository, while the actual files are stored in a separate LFS cache.
After running the git lfs
track command for your specific path or directory, Git LFS will modify the g.gitattributes
file in your repository to include the new tracking paths. Make sure to commit changes to the g.gitattributes
file.
Cloning and Pulling from a Repository with LFS Files
When cloning or pulling from a repository that uses Git LFS, the LFS files are automatically downloaded.
Listing Tracked Large Files
1 |
git lfs ls-files |
This command lists the files currently tracked by Git LFS in your repository, along with their sizes and the SHA-256 hashes of the versions of those files that are checked out in your working copy.
You can also manually inspect the .gitattributes
file in your repository’s root directory. This file contains the patterns for files tracked by Git LFS. For example, you might see lines like:
1 |
*.psd filter=lfs diff=lfs merge=lfs -text |
Cloning a Repository with LFS Files
When you clone a repository that uses Git LFS, If Git LFS is installed, LFS-tracked files should be automatically downloaded when you clone the repository. However, if this does not happen, you might need to manually fetch the LFS files:
1 2 |
git lfs fetch git lfs checkout |
– git lfs fetch
: downloads the LFS files for your current branch from the remote.
– git lfs checkout
: replaces the placeholder pointers in your working directory with the actual large files.