Completely Remove a File from Pushed git History

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:

https://help.github.com/en/github/authenticating-to-github/removing-sensitive-data-from-a-repository

And the BGF itself is available here:

https://rtyley.github.io/bfg-repo-cleaner/

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.