Search code examples
gitgit-filter-branch

Delete all previous versions of file in git repo


I have a single file in a git repo and it contains sensitive information. I've removed this information in the latest commit. Now I want to delete all previous versions of this file from the git repository. How can this be done?

I believe it can be done with git-filter-branch but I haven't come across an example I can wrap my head around yet.


Solution

  • Quoting the git documentation:

    Suppose you want to remove a file (containing confidential information or copyright violation) from all commits:

    git filter-branch --tree-filter "rm filename" HEAD
    

    However, if the file is absent from the tree of some commit, a simple rm filename will fail for that tree and commit. Thus you may instead want to use rm -f filename as the script.

    So you want to run git filter-branch --tree-filter "rm -f filename" HEAD to erase all history of that file