跳到主要内容

Git LFS

提示

Git LFS refers to Git's Large File Storage, which is a tool for effectively managing 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 retaining only references to these files in the repository. This approach prevents the repository from becoming too large due to frequent saves of diffs, accelerates the cloning and downloading processes of the repository, and ensures that version control of large files does not cause performance issues.

By using Git LFS, you can achieve:

  • Separate Large Files: LFS allows large files to be separated from the code repository and stored in a separate location.
  • Version Control: LFS still provides version control but retains only references to the files in the repository to prevent it from becoming too large.
  • High Performance: LFS can accelerate the cloning and downloading processes of the repository because it doesn't need to transfer large files every time.
  • File Locking: LFS supports file locking to avoid conflicts when multiple users simultaneously edit large files.

Git LFS

Git LFS Download and Installation


Note: Installing Git LFS requires a Git version of at least 1.8.5.

Git LFS Official Website: https://git-lfs.github.com/

GitCode Mirror Address: https://gitcode.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

Running git lfs install, if it shows "Git LFS initialized", indicates successful installation.

MacOS System

  1. Install HomeBrew
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Running git lfs install, if it shows "Git LFS initialized", indicates successful installation.

  1. Install Git LFS
$ brew install git-lfs
$ git lfs install

Windows System

  1. Download and run the Windows installer
  2. Run the Windows installer
  3. Execute git lfs install in the command line

Configuration


Configure the file types we want to associate with Git LFS; this information will be added to the .gitattributes file in the repository.

If you're not familiar with the role of the .gitattributes file, 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 .jpg files with Git LFS:

$ git lfs track "*.png"

This creates a .gitattributes file containing 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 that 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


List all patterns currently being tracked by Git LFS

$ git lfs track

List all files currently tracked by Git LFS

$ git lfs ls-files

Untracking and Removing Files from LFS


Untrack all specific file types from LFS 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, 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 LFS features:

  • Use Only for Large Binary Files: Use LFS only for managing large binary files, not text files.
  • Enable File Locking: Enable file locking in collaborative environments to prevent multiple users from simultaneously editing large files.
  • Regularly Clean Up Unnecessary Large Files: Regularly review the repository and remove references to large files no longer needed.
  • Provide Documentation: Provide documentation or guidelines to team members on how to use LFS to ensure correct usage.

Thanks to @BaiXuePrincess for contributing the help documentation on Git-LFS.

Project Address: https://gitcode.net/BaiXuePrincess/git-lfs