I had a need to take a folder in one git repository and create a whole new git repository with it, preserving history for all files inside. My desire to keep git history made the process a bit more complicated than simply copying the directory into a new git repository.
First, create a new folder on the git server. I’m all command line, no GUI yet, so I need to make it a bare repository. (Thanks to geeksforgeeks on how to to do this)
#On the main git "server" mkdir <reponame>.git cd <reponame>.git git init --bare
Now, on the desktop (not the git server) clone a copy of the repository with your desired folder into a new directory, remove the git origin server, then strip out everything except that directory (thanks to gbayer.com for the info)
#On the desktop git clone <initial git repository url> <new_directory_name> cd <new_directory_name> git remote rm origin git filter-branch --subdirectory-filter <directory_to_keep_history_of> -- --all
Lastly, (still on the desktop in the new repository directory) create a new origin with the path of the new repo you created above
#On the desktop, inside new_directory_name git remote add origin <server>:<path_to_new_repo_folder> git push --set-upstream origin master