Get Rid of Some File From Git History

Imagine you just realized you added a long time ago in your git repository some files that don’t belong here, like ./target.

Use filter-repo to get rid of them

sudo apt install git-filter-repo

Get a fresh clone

git clone url/repo && cd repo

Then, rewrite the history to remove the target path

git filter-repo --path target --invert-match

Then push the result back

git remote add origin url/repo
git push --force-with-lease origin branch branch

Warn other people on your team so that they won’t mistakenly merge their old history with the new one and push back the old content.

base="$(git merge-base origin/mybranch mybranch)"
git fetch
git rebase $base mybranch --onto origin/mybranch