How to Remove Large Files from Git History and Reduce Repository Size

How to Remove Large Files from Git History and Reduce Repository Size

Efficiently Resolving Git's Big File Problem and Safeguarding Your Repository

Introduction: Accidentally tracking and committing large files in your Git repository can lead to issues, including exceeding platform-specific size limits like GitHub's 100MB limit. This can be a common problem, especially when working on projects with multiple platforms or architectures. In this article, we will walk you through the process of removing large files from your Git commit history, ensuring a clean and efficient repository. Whether you're a seasoned developer or just starting with Git, this guide will help you resolve the issue.

Understanding the Problem

Let's set the scene: You're working on a Flutter project and, unintentionally, you include a large Windows build file in your repository. Now, you need to remove this file from all the commits to avoid exceeding GitHub's file size limit and reducing your repository's size. Here's how you can solve the problem step by step.

Solution: Using git-filter-repo

To remove a large file from your Git commit history, you can use a powerful tool called git-filter-repo. Here's a breakdown of the steps you need to follow:

1. Install git-filter-repo

Before you start, make sure you have git-filter-repo installed. You can do this with the following command:

python -m pip install --user git-filter-repo

2. Exclude the Problematic File

Use git-filter-repo to exclude the large file from your Git history. In your case, the file's path was build/windows/firebase_cpp_sdk_windows_11.6.0.zip. You can exclude it using this command:

git filter-repo --path build/windows/firebase_cpp_sdk_windows_11.6.0.zip --invert-paths

This command inverts the path filter, effectively removing the specified file from your commit history.

3. Add a Remote (if necessary)

As a precaution, it's a good practice to re-add your remote repository to your Git configuration. The filtering process might affect your remote repository's history. To add a remote, use the following command:

git remote add origin <remote_repository_url>

Replace <remote_repository_url> with the URL of your remote repository.

4. Force Push the Changes

After excluding the file and adding a remote repository, you need to push the changes to your remote repository. Be cautious when using the --force option, as it rewrites history. Here's the command:

git push --force origin main

This command will update your remote repository with the filtered history.

Conclusion

By following these steps, you can successfully remove large files from your Git commit history, ensuring that they no longer contribute to your repository's size and avoiding issues with size limits on platforms like GitHub.

Whether you're building a Flutter project or working on any other Git-managed codebase, understanding how to handle large files and manage your repository's history is a valuable skill. Keep in mind that using git-filter-repo should be done with caution, as it rewrites history, and it's always a good practice to ensure your remote repository reflects the changes correctly.

In a world where accidental commits happen, knowing how to clean up your repository can save you from headaches down the road. This article has provided you with a clear, step-by-step guide to resolve the issue of large files in your Git history.

Did you find this article valuable?

Support Rohit Gupta by becoming a sponsor. Any amount is appreciated!