Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cyberang3l
Created April 12, 2016 14:55
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save cyberang3l/6012c82266122e05db33f4cb8dcf598b to your computer and use it in GitHub Desktop.
Save cyberang3l/6012c82266122e05db33f4cb8dcf598b to your computer and use it in GitHub Desktop.
GIT: How to extract a specific folder from a git repository branch, including the folder's related git history only

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment