DEV Community

Cover image for How to push large files to git repository using Git LFS
Unegbu Clinton
Unegbu Clinton

Posted on

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)