I will not write an introduction of why we need to do this. This is how I did it, not sure if it is the best way but worked 100%:
I used the git commands described in this great article https://help.github.com/articles/remove-sensitive-data/
- Two repos: REPO_OLD with the folder I want to move, REPO_NEW an empty repository where I will move the folder
- Copy everything (.git folder included) from REPO_OLD to REPO_NEW (another way is to clone REPO_OLD into a new folder REPO_NEW)
- DONT FORGET THIS: Remove the origin in REPO_NEW: git remote rm origin
- Delete in REPO_NEW all folders you DONT need. E.g. folder name is FOLDER then
git filter-branch --force --index-filter ' git rm --cached --ignore-unmatch -r FOLDER/ ' --prune-empty --tag-name-filter cat -- --all
do it with all the folders one by one, at the end only the folder you need should be in REPO_NEW. If some folders still exist it is due to some files were not added to git, you can delete the folders manually, they are not in the git history anymore.
- Now set origin in REPO_NEW to the remote repository where you want to push this new repo.
E.g. git remote add origin firstname.lastname@example.org:someuser/REPO_NEW.git
- Push everything forcing overwrite history: git push origin --force --all
- Ok, it is copied, but not moved. Just delete the folder in REPO_OLD using the same command you used in step 4.