This post explains how to setup a new repository from a sub-directory of an existing git repository.
Clone the repository that contains the sub-directory:
git clone https://github.com/USERNAME/REPOSITORY-NAME
Change the current working directory to your cloned repository:
run git filter-branch, To filter out the subfolder from the rest of the files in the repository
git filter-branch --prune-empty --subdirectory-filter FOLDER-NAME BRANCH-NAME
FOLDER-NAME: The folder within your project that you'd like to create a separate repository from.
BRANCH-NAME: The default branch for your current project, for example, main or gh-pages
Create a new repository on GitHub and set up a new remote URL for your new repository
git remote set-url origin https://github.com/USERNAME/NEW-REPOSITORY-NAME.git
Push your changes to the new repository on GitHub.
git push -u origin BRANCH-NAME
When you use git filter-branch, a backup file is created in refs/original/refs/heads/master
If you used the command in branch master. You can check if you have the backup in .git/refs directory. With this in mind, you can use this backup to recover your files with:
git reset --hard refs/original/refs/heads/master
then delete the folder, commit and push the changes.