Rainbow Coding

Introducing How to rewrite git history and remove files

If you accidentally have committed huge files or sensitive data to git, there is fairly easy way to remove it cleanly from history.

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.

git filter-branch --index-filter 'git update-index --remove bin/' 123456abcdef..HEAD

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.

rm -Rf .git/refs/original
rm -Rf .git/logs/
git gc --prune now

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.

© RainbowCoding 2010-2024