It appears that the up-to-date answer to this is to not use filter-branch
directly (at least git itself does not recommend it anymore), and defer that work to an external tool. In particular, git-filter-repo is currently recommended. The author of that tool provides arguments on why using filter-branch
directly can lead to issues.
Most of the multi-line scripts above to remove dir
from the history could be re-written as:
git filter-repo --path dir --invert-paths
The tool is more powerful than just that, apparently. You can apply filters by author, email, refname and more (full manpage here). Furthermore, it is fast. Installation is easy - it is distributed in a variety of formats.