How to Remove a Large File from Git

So, you’re managing a project and go to inspect your .git/ file. 2GB!? Whaa—

Solution

I don’t care about an explanation; I just want it gone and out of my life. Say no more! Run the following and be done with it:

git filter-branch --index-filter 'git rm -rf --cached --ignore-unmatch *.psd' -- --all
	

You’ll see some output notifying you it’s deleting files. If nothing outputs, nothing was deleted. Continue:

git pack-refs --all --prune
	git reflog expire --expire-unreachable=now --all
	git repack -A -d
	git gc --aggressive --prune=now
	

… And, done! That’s it: behold your repo, without those hideous .psd files.

Warnings

Note that the example code removes all .psd files recursively. So if instead of *.psd, you accidentally ran *, you’re freaking toast. Be careful. A better alternative, especially for specific files, would be this:

git filter-branch --index-filter 'git rm --cached --ignore-unmatch path/to/your/file' -- --all
	

Longer Explanation

What this does is recursively checkout your repo at every commit, run that command, then re-commit. Basically rewriting history. As you might imagine, looping through your entire history might take some time, and in bigger repos, you’d be right.

You can run any command within the quotes, but the given command works wonders for any rogue files that creeped their way into your git repo and stank up the place.

In my specific instance, a mess of unnecessary .psd files were accidentally included with a git add * by someone else in the initialization, and I had been working with the repo for some time without noticing the filesize. I had been working on it for a couple months, and this particular commit in question stretched back 2 years from present date. Needless to say it couldn’t be ignored.

Running this turned by repo from 2GB down to 93MB in just a couple minutes! Naturally, it wasn’t the only file, so after cycling through the process a few times with some binary files that truly didn’t need to be tracked, I eventually got my Repo down to literally 1% of what it was.

Credit

Full credit of this method goes to this blog post. Be sure to scroll down to Commands and Output; if you start at the top it won’t go so well.

Further References

Drew Powers is a frontend developer at Envy.