Completely Remove a File from Pushed git History

Chris Achard
InstructorChris Achard

Share this video with your friends

Send Tweet

If we want to completely remove a file from github - including all history - there is a tool that we can use called the BFG.

The github help article is here:

And the BGF itself is available here:

We'll start by downloading the BFG jar file, and then cloning a mirror of our repo with:

git clone --mirror [repo-url]

Then we can delete our .env file with:

java -jar ~/Downloads/bfg-1.13.0.jar --delete-files .env my-repo.git

which will delete the .env file. Then we can use the following command to prune the entire history and garbage collect the remains:

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

And finally, use git push to push that change to github, and remove the .env file from all of the history on github as well.