It can happen that you add some big files to git (binaries, documents, images, ...) or some "private" files which should not be in git. Removing it from your filesystem with a single commit will just remove the file in the working tree, but Git does not fully delete a file when removing it via commit. Maybe you want to restore it. That's why Git keeps it in his history.
Remove the last commit
If your file was added with the last commit, its pretty straight forward to remove it from the
git reset --hard HEAD^
Remove from history
It is not the last commit? It was added some time ago and you now have to remove it from the history too? Just to be sure you can't restore it?!
The Git command
filter-branch rewrite revision history and can modify each tree or information about each commit. To remove a file or a whole directory from Git history you have to execute the following command:
git filter-branch -f --index-filter "git rm -rf --cached --ignore-unmatch path/to/file-or-dir" --prune-empty --tag-name-filter cat -- --all
Depending on your amount of commits and the size of your repository it can take some time to finish.
Push to your remote
If you have already pushed the files to a remote host, you need to force the push to update all rewritten commits.
Be aware that a forced push can be dangerous if you are working with other developers/people. Best use-case would be that the others clone the repository again from scratch. As far as that's possible.
To force a push just execute this command:
git push origin --force --all
Force garbage collection
The space will not be freed immediately. You can either wait for Git garbage collection to run or you can force it with the following command:
git gc --prune=now --aggressive