Permanently Remove Any Record of a File From git

Permanently Remove Any Record of a File From git

A co-worker and I needed to remove all instances of a binary blob from one of our git repos a while back. This was a lot harder than I thought it should have been, in my opinion. However, distributed version control is not really designed to easily allow folks to delete every instance of a file. But, if you ever run into a case where you do need to expunge a file in git here is how we did it.

Get a new clone of the repo (in scratch/temp space):
git clone REPO_LOCATION

Detach it from the remote origin:
git remote rm origin

Remove the file and rewrite history:
git filter-branch --index-filter ‘git rm --cached --ignore-unmatch FILENAME’ HEAD

Remove garbage and lingering files:
git reflog expire --expire=now --all && git gc --prune=now --aggressive

Replace the centralized repo (aka REPO LOCATION) and any copies others might be using (everyone should re-clone the newly replaced repo)

There are several guides and HOWTOs that we stumbled across to do this; those might work better for your use case (Google is your friend). But, this five step process worked very well for us.

Sources:
http://stackoverflow.com/questions/9224754/how-to-remove-origin-from-git-repository
http://git-scm.com/docs/git-filter-branch
https://rtyley.github.io/bfg-repo-cleaner/

Chris Short

Chris Short

https://chrisshort.net

View Comments