https://stackoverflow.com/a/64563565/2634595 https://github.com/newren/git-filter-repo

Use the third-party add-on git-filter-repo

# 1. fresh clone of the repo. Make sure you have a backup clone somewhere else in case something goes wrong.
$ mkdir tmpdir && cd tmpdir && git clone <repo>
# 2. make sure remote origin is configured in .git/config
# 3. multiple paths can be specified by using multiple --path parameters
$ git filter-repo --invert-paths --path <path to the file or directory>
# 4. push changes to origin
$ git push origin --force --all

Why filter-repo is better than filter-branch