Removing file history from Git

Simple scenario – you have commited .env file to repository. File has more or less sensitive data. You want to remove it from repo and remove any history of this ever happened.

Before doing this, keep local copy of the file just in case.

git filter-branch --force --index-filter "git rm --cached --ignore-unmatch .env" --prune-empty --tag-name-filter cat -- --all

After that

git push origin --force --all

And add file location to .gitignore so you won’t do same mistake again.

Leave a Reply

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