Sometimes we mistakenly commit large files or files that contain passwords, so we need permanently delete files from Git history.

Clear wrong files from your working directory

git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch path-to-your-remove-file' --prune-empty --tag-name-filter cat -- --all

path-to-your-remove-file is the relative path of the file you want to clear.

Do not let the path start with ‘/‘ which will make it be considered to be in the install dir of Git.

If you want to clear a directory, add -r after git rm --cached.

The following message is successfull info.

Rewrite e9a2f6be143f4f743f72800821e92c4aa2da7a0d (15/16)
Ref 'refs/heads/master' was rewritten

Meanwhile the following message is NO SUCH FILE info.

WARNING: Ref 'refs/heads/master' is unchanged

Push changes

Compulsively push the changes to remote.

git push origin master --force

Push Git tags.

git push origin --tags --force

Clean-up and recovery space

rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --prune=now
git gc --aggressive --prune=now

Refer:
[1]Git如何永久删除文件(包括历史记录)
[2]彻底删除 Git 仓库中的文件避免占用大量磁盘空间

Related:

 TOC