跳到主要内容

Git LFS

提示

Git LFS stands for Git Large File Storage, which is a tool designed 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 the references to these files in the repository. This approach avoids bloating the repository size 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.

With Git LFS, you can achieve:

  • Separation of Large Files: LFS allows separating large files from the code repository, storing them in a separate location.
  • Version Control: LFS still provides version control but retains only the references to files in the repository to avoid excessive repository size.
  • High Performance: LFS can accelerate the cloning and downloading processes of the repository since it doesn’t need to transfer large files every time.
  • File Locking: LFS supports file locking to prevent conflicts when multiple users simultaneously edit large files.

Git LFS

Git LFS Download and Installation


Note: Installing Git LFS requires Git version 1.8.5 or higher.

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 will show "Git LFS initialized" if installed successfully.

MacOS System

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

Running git lfs install will show "Git LFS initialized" if installed successfully.

  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 unsure about 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 the .gitattributes file with the following content:

*.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 modifications to .gitattributes must also be committed to the repository like other changes:

$ 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 being tracked by git lfs

$ git lfs ls-files

Untracking and Removing Files from LFS


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

  • Only Use 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 delete references to large files that are no longer needed.
  • Provide Documentation: Provide documentation or guidelines to 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