How to remove file from Git history?
I have found this answer and it helped:
git filter-branch --index-filter \
'git rm -rf --cached --ignore-unmatch path_to_file' HEAD
Found it here https://myopswork.com/how-remove-files-completely-from-git-repository-history-47ed3e0c4c35
If you have recently committed that file, or if that file has changed in one or two commits, then I'd suggest you use rebase
and cherrypick
to remove that particular commit.
Otherwise, you'd have to rewrite the entire history.
git filter-branch --tree-filter 'rm -f <path_to_file>' HEAD
When you are satisfied with the changes and have duly ensured that everything seems fine, you need to update all remote branches -
git push origin --force --all
Note:- It's a complex operation, and you must be aware of what you are doing. First try doing it on a demo repository to see how it works. You also need to let other developers know about it, such that they don't make any change in the mean time.