Photo by @Franck V. on UnSplash

Intro

Sometimes it's an accidental commit of sensitive data, or sometimes it's just an urge to get rid of the ancient poorly-written code. Luckily, Git does offer a way to change history:

cd /path/to/git/repo/directory

git filter-branch --force --index-filter \
'git rm -rf --cached --ignore-unmatch ./dir/to/remove' \
--prune-empty --tag-name-filter cat -- --all

Next step

Now what you want to do is to make sure these data never get pushed again, so adding them to your .gitignore file would be a good ieda:

echo "YOUR-FILE-WITH-SENSITIVE-DATA" >> .gitignore
git add .gitignore
git commit -m "Avoiding stupidity with .gitignore"

Finally, a force push to resolve all the problems (force push against tags as well to remove from tagged releases):

git push origin --force --all

git push origin --force --tags