Git LFS
Git LFS stands for Git Large File Storage, a tool used to effectively manage large binary files. It allows developers to separate large files from the code repository, such as images, audio, video, and data files, storing them in a separate storage area and keeping only references to the files in the repository. This avoids the repository becoming too large due to each save's diff, speeds up the cloning and downloading of the repository, and ensures that version control of large files does not cause performance issues.
With Git LFS, you can achieve:
- Separating large files: LFS allows large files to be separated from the code repository and stored in a different location
- Version control: LFS still provides version control, but only keeps file references in the repository to avoid it becoming too large
- High performance: LFS can speed up the cloning and downloading of the repository because it doesn't need to transfer large files every time
- File locking: LFS supports file locking to prevent conflicts caused by multiple users editing large files at the same time
Git LFS Download and Installation
Note: Installing Git LFS requires Git version no lower than 1.8.5
Git LFS official website: https://git-lfs.github.com/
Project source code address: https://github.com/git-lfs/git-lfs
Linux System
$ curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
$ sudo apt-get install git-lfs
$ git lfs install
Run
git lfs install
, if it shows "Git LFS initialized", it means the installation is successful.
MacOS System
- Install HomeBrew
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Run
git lfs install
, if it shows "Git LFS initialized", it means the installation is successful.
- Install Git LFS
$ brew install git-lfs
$ git lfs install
Windows System
- Download the Windows installer
- Run the Windows installer
- Execute
git lfs install
in the command line
Configuration
Configure the file types you want to associate with Git LFS. This information will be added to the .gitattributes
file in the repository.
If you are not familiar with the purpose of the .gitattributes
file, you can refer to this document.
The simplest way to associate file types with Git LFS is through the git lfs track
command.
For example, to manage all .png files with Git LFS:
$ git lfs track "*.png"
The .gitattributes
file has been created and contains the following information:
*.jpg filter=lfs diff=lfs merge=lfs -text
Perfect! From now on, LFS will handle this file. Now, we can add it to the repository as before. Note, any changes to .gitattributes
must also be committed to the repository, just like other modifications:
$ git add .gitattributes
$ git add design-resources/design.psd
$ git commit -m "Add design file"
Common Git LFS Commands
View the list of all patterns currently being tracked by git lfs
$ git lfs track
View the list of files currently being tracked by git lfs
$ git lfs ls-files
Untrack and Remove Files from LFS
Untrack all files of a specific type and remove them from the cache:
$ git lfs untrack "*file-type"
$ git rm --cached "*file-type"
If you want to re-add these files to regular git tracking and commit them, you can do the following:
$ git add "*file-type"
$ git commit -m "restore "*file-type" to git from lfs"
Best Practices
Here are some best practices for using the LFS feature:
- Use only for large binary files: Only use LFS to manage large binary files, not text files
- Enable file locking: In a multi-user collaboration environment, enable file locking to prevent multiple users from editing large files simultaneously
- Regularly clean up unnecessary large files: Regularly review the repository and delete references to large files that are no longer needed
- Provide documentation: Provide documentation or guidelines for team members on how to use LFS to ensure proper usage
Thanks to @BaiXuePrincess for contributing the help documentation on Git-LFS
Project address: https://gitcode.net/BaiXuePrincess/git-lfs