How to rewrite git history and remove files

If you have a git repository that contains files with sensitive information or large binary files, you probably want to remove those files before publishing the repository for larger audience.

This is actually very simple thing to do with git. Git has a feature to rewrite the history, recreating the repository commit by commit, while applying some filters on each commit. In practice it requires everyone working in that repository to remove it and clone it again. Here’s how to do it.

For example, you have accidentally committed a bin directory in revision 123456abcdef.
First go to the git directory and run.

This rewrites whole repository history from 123456abcdef to HEAD, ignoring bin directory. This leaves the files still into history, so following commands are necessary to actually delete them from .git directory.

Git’s filter-branch can also be for many other things, such as changing commit author, or moving directories in history. Just remember that it works only if your repository or changes have not been published to public yet.

Leave a Reply