How to move a directory from one git repo to another (or new) without losing history

Make copy of repo git clone dirtySourceRepo newSourceRepo

OR clone from actual git repo and prevent push git remote set-url --push origin no_push

  • Make sure to checkout the correct branch before the next step.

  • Cloning from another local directory allows you to easily start over if you make a mistake and also prevents you from accidentally pushing upstream.

use github.com/newren/git-filter-repo to filter down to the folder you actually care about git filter-repo --subdirect-filter /subdirectory/I/want/to/extract --to-subdirectory-filter the/new/folder/I/want/it/in This moves the subdirectory to the root of your repo. You can use different variations of this if you want to. You can omit --to-subdirectory-filter if you want the contents of the folder to be in the root of the repo.

In destination repo (which can be a new empty repo or an existing one), set a new remote to the freshly filtered source repo. git remote add source ../newSourceRepo

Make a new branch to put the stuff into git checkout -b newBranch

Then pull in the content from the source repo: git pull source master --allow-unrelated-histories