Git LFS: управление большими файлами в Git
Git LFS (Large File Storage)
Git LFS (Large File Storage) is an extension for the Git version control system developed by GitHub. Its main goal is to facilitate the management and storage of large files in Git repositories. As we know, Git becomes slow and takes up a lot of space when working with large files, such as images, videos, audio, and others. Git LFS solves this problem by allowing more efficient handling of such files.
How to use Git LFS?
Firstly, you need to install Git LFS on your machine. To do this, download the corresponding version from the official website git-lfs.github.com and follow the installation instructions.
After installation, you can start using Git LFS. Adding a file to the Git LFS repository is done using the "git lfs track" command. For example, if you need to track files with the ".mp4" extension, execute the following command:
git lfs track "*.mp4"
Now Git LFS will automatically handle and track all files with the ".mp4" extension.
Once the files have been successfully marked for tracking by Git LFS, they can be added to the repository using regular Git commands, for example:
git add filename.mp4
git commit -m "Added video file"
git push
When executing the "git push" command, Git LFS will transfer the files to a separate storage (GitHub server or other LFS server), and only a link to the large file will remain in the repository.
When another user clones a Git LFS repository, they need to execute the "git lfs pull" command to retrieve all the large files. For example:
git clone <repository_url>
cd repository_name
git lfs pull
The "git lfs pull" command will download all tracked files to the local repository folder.
These are just the basic Git LFS commands. Additional options include using the "git lfs ls-files" command to view all files tracked by Git LFS, as well as the "git lfs migrate" command to migrate an existing repository to Git LFS and the "git lfs status" command to view the current state of files.
A code example that uses Git LFS is the following Python script fragment, which uploads a video file to the repository using Git LFS:
import os
import subprocess
def add_file_to_repo(filename):
# Mark the file for Git LFS tracking
subprocess.run(["git", "lfs", "track", filename])
# Add the file to the Git repository
subprocess.run(["git", "add", filename])
# Commit the changes
subprocess.run(["git", "commit", "-m", "Added file " + filename])
# Push the changes to the remote repository
subprocess.run(["git", "push"])
# Usage example
video_file = "example.mp4"
add_file_to_repo(video_file)
In this example, the "example.mp4" file will be marked for Git LFS tracking, added to the repository, committed, and pushed to the remote repository.
Git LFS significantly simplifies the management of large files in Git repositories, making working with them more efficient. This is especially useful for projects that require storage and exchange of large media files. However, keep in mind the limitations of Git LFS related to file size, available space on hosting, and network bandwidth.