Remove Folder from Git with History

I was just cleaning up my desktop and came across a snippet I had saved which removes a specified folder from a Git repository including removing it from the history of the repository. In general, if something was committed to a repository, you shouldn’t remove it from the history, but there are cases where it makes sense – perhaps a sensitive password or key was unintentionally commited.

The command is pretty simple, but be warned that it can have grave consequences if you remove the wrong thing:

git filter-branch --force --index-filter \
'git rm -r --cached --ignore-unmatch .idea' \
--prune-empty --tag-name-filter cat -- --all

git push origin master --force

In the example above, I removed the default .idea folder that is included with JetBrains IDEs like IntelliJ and Pycharm.


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *