Git ships with an advanced command called filter-branch
which can be used to, among other things, split out a new Git repo from a larger Git repo. Unfortunately, the tool has issues and is officially not recommend by Git. They recommend https://github.com/newren/git-filter-repo as an alternative. git-filter-repo
seems like it is worth exploring for use cases beyond the most basic happy path. Fortunately, I have only needed the basic happy path of "split out this sub-directory and all its commit history exactly as is into its own Git repo". For a good illustration of this use case, see https://docs.github.com/en/github/using-git/splitting-a-subfolder-out-into-a-new-repository.
The core command is:
git filter-branch --prune-empty --subdirectory-filter FOLDER-NAME BRANCH-NAME