I assume that you want to erase the file from history as if it never existed. If you just want to get rid of the file in the future, then use git rm my-bad-file.txt and ignore the rest of my answer.
If the file is only in your last commit, the easiest way is to use git rm my-bad-file.txt to delete the file, and then git commit --amend to edit the previous commit.
If there are several commits containing an intruder file, git filter-branch may be useful.
git filter-branch --index-filter 'git rm --cached --ignore-unmatch my-bad-file.txt' master..abc
It means:
git filter-branch : rewrite some commits!
--index-filter : change each commit index without actually checking it on disk.
'git rm --cached --ignore-unmatch my-bad-file.txt' . For each commit, do not run the file named "my-bad-file.txt" if it exists.
master..abc : do this in all commits on the abc branch back to where it branches from the main branch.
source share