"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to push large files to git repository using Git LFS

How to push large files to git repository using Git LFS

Published on 2024-11-02
Browse:784

How to push large files to git repository using Git LFS

So I was working on a project recently where I had to work with high-resolution assets, and as a result, they were large in size. I couldn't push the files to my repository. Whenever I tried, I received the following error:

remote: error: File file.csv is 182.47 MB; this exceeds GitHub's file size limit of 100.00 MB
remote: error: GH001: Large files detected. You may want to try Git Large File Storage - https://git-lfs.github.com.

Now let's talk about git extension Git LFS and how to utilize it.

Git Large File Storage (LFS) is a Git extension designed to handle large files in your repository more efficiently.

1. What is Git?

Git is a version control system that lets you track changes to your code and collaborate with others. Normally, Git saves all the content (files, code, etc.) directly into your repository, including any images, videos, or large binary files.

2. Why Do We Need Git LFS?

By default, Git stores everything, but large files like high-resolution images, videos, or datasets can make your repository very big and slow. Every time you push or pull changes, Git downloads all the data, which takes time and uses more storage space on your machine.

Git LFS is designed to handle these large files more efficiently by:

  • Storing large files separately: Instead of saving large files directly in your repository, Git LFS replaces them with pointers (small references). The actual file is stored outside your repo in a separate location.

  • Keeping the repo light: This way, your repository stays lightweight and fast because Git only downloads the large files when you need them.

3.How Does Git LFS Work?

When you add a large file (e.g., a video or image) to your repository, Git LFS replaces it with a small pointer file.

The pointer file tells Git LFS where the large file is actually stored.
When someone else clones your repository or pulls changes, they’ll get the pointer file instead of the large file itself unless they explicitly pull down the large files.

4. Getting Started with Git LFS

To start using Git LFS in your project, follow these steps:

1.Install Git LFS: You need to install Git LFS on your machine. For most systems, this is done by running:

git lfs install

2.Track Large Files: Once installed, you need to tell Git LFS which files to track (e.g., all .png files or _.mp4 _files). This is done using the git lfs track command:

git lfs track "*.png"

This tells Git LFS to track all files ending with .png.

3.Commit the Changes: After running the track command, Git LFS creates a .gitattributes file that defines the rules for which files to track. You need to commit this file to your repository:

git add .gitattributes
git commit -m "Track large files using Git LFS"

4.Add and Push Large Files: Now, when you add a large file (e.g., a video or image), Git LFS will automatically manage it:

git add largefile.mp4
git commit -m "Add large video file"
git push

Note
Make sure you have initialized and set up LFS before committing your large files, else git LFS would not track the file when you push. And you would continue to get the error as before. I also encountered this problem when I first started using the git LFS extension.

5. Pulling Large Files:
When someone clones or pulls your repository, they will get the lightweight pointer files by default. If they want the actual large files, they can pull them down with:

git lfs pull

6. Benefits of Git LFS

  • Speeds up Git operations: Your repository stays fast because Git doesn’t have to handle large files directly.

  • Reduces storage use: Large files are stored separately, which reduces the size of your repo.

  • Efficient collaboration: Only the necessary large files are downloaded when needed.

7. Drawbacks of Git LFS

  • Storage limits: Some platforms (like GitHub) provide limited free storage for LFS, and you may need to pay for extra storage if you have a lot of large files.

  • Slower pulls with large files: If you have a lot of large files and download them all at once, it can still take time, though Git LFS optimizes this compared to regular Git.

Feel free to add comments or questions. I'll gladly reply them

Release Statement This article is reproduced at: https://dev.to/unegbuclinton/how-to-push-large-files-to-git-repository-using-git-lfs-5db3?1 If there is any infringement, please contact [email protected] delete
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3