Remove sensitive files in git history
- Filter the branch to remove the sensitive files/folders (eg.
FILE1
andFOLDER1
) 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.
- 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.
- Clean these references (eg, on the branch
main
):
git update-ref -d refs/original/refs/heads/main