How to remove a file from Git history but keep it in a project

Sometime happen that we forget properly configure .gitignore file and keep tracking some file with sensitive data.

Firstly, remove a file from staging area:

$ git rm --cached <filename>

Then install git-filter-repo from here https://github.com/newren/git-filter-repo, e.g.:

$ pip install git-filter-repo

And finally, remove the file from all branches and all commits:

$ git filter-repo --invert-paths --path <filename>

@source:
https://github.com/newren/git-filter-repo/blob/main/Documentation/converting-from-filter-branch.md#removing-a-file

Leave a Reply