Removing sensitive data from Git history


Hello folks, this will be a quick but really handy post. Let's say we are working on a side project that we didn't care the sensitive data like credentials of API keys and we want to make the repo public on Github.

I've found a great tool to clear all the commit history of the specific file, in my case it was .env.example.

The library is called git-filter-repo and it is developed with python.

So, to use the library, first we need to install the python script using:

pip install git-filter-repo

and then we are running this code to delete all the history of the selected file:

git filter-branch --force --index-filter "git rm --cached --ignore-unmatch <the path of file that you want delete>" --prune-empty --tag-name-filter cat -- --all

When you run the code, you will see the remote branch and local are different. Now, you just need to push your local changes with this command:

git push origin --force --all

That's it! Now, when you check the Github, you wont't see any history about that file.

Hope this will help someone having the same issue.