Removing files from git history

I recently cloned a GIT repo that was over 1Gb in size. This was due to someone accidentally pushing the Nuget packages folder.

To make matters worse there were lots of branches all of which had these Nuget packackes. One option to remove the packages from all branches is as follows:-


git clone REPO_URL
git filter-branch -f --index-filter "git rm -rf --cached --ignore-unmatch packages" -- --all

git push -all -f

A faster and better option is to useĀ BFG Repo cleaner

If you don’t already have installed, follow the instructions on the above site or use brew as below:-


brew install bfg

Clone the repo as a mirror and use the BFG delete-folders command:-


git clone --mirror REPO_URL
bfg --delete-folders packages REPO.git

Now free up the space and push


cd REPO.git

git reflog expire --expire=now --all && git gc --prune=now --aggressive

git push

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.