GIT: How to extract a specific folder from a git repository branch, including the folder's related git history only
NOTE: If you want to keep the history for a specific folder in the master
branch, just skip steps in lines 3,4,5,6,7
git clone <git-repository-url>
cd <git-repository-dir>
git checkout <branch-name> # line 3; Checkout the branch of interest
git merge --strategy=ours master # line 4; keep the content of this branch only and record a merge
git checkout master # line 5; Go back to the master branch
git merge <branch-name> # line 6; Merge the interested branch to the main branch
git branch -D <branch-name> # line 7; Delete the merged branch
git remote rm origin
git filter-branch --subdirectory-filter <dir-of-interest> -- --all # This line is doing the magic
git reset --hard
git gc --aggressive
git prune
cd ..
mv <git-repository-dir> <dir-of-interest> # Rename the repository dir to something that reflects the new repository contents