Remove sensitive files in git history

Source: https://stackoverflow.com/questions/6403601/purging-file-from-git-repo-failed-unable-to-create-new-backup

  1. Filter the branch to remove the sensitive files/folders (eg. FILE1 and FOLDER1) from history:
git filter-branch --index-filter 'git rm -rf --cached --ignore-unmatch FILE1 FOLDER1' HEAD

Note: I don’t know how to adapt if there are several branches.

  1. Push the changes to the branch of interest (eg. main):
git push --force --set-upstream origin main

From StackOverflow:

After filter-branch, Git keeps refs to the old commits around, in case something goes wrong.

  1. Clean these references (eg, on the branch main):
git update-ref -d refs/original/refs/heads/main