Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using git filter-branch to remove everything except one subdirectory

I have been trying to use git to remove everything except the source code in a particular branch. The

git filter-branch -f \
    --subdirectory-filter source_code \
    --prune-empty \
    --tag-name-filter cat -- --all

command gets me basically what I want, except it places the files in source_code/ at the root, whereas I want them all to remain in the directory. I.e.

- source_code
    - file1.py
    - file2.py

not just

- file1.py
- file2.py

How can I do this?

like image 858
GDYendell Avatar asked Aug 09 '16 09:08

GDYendell


1 Answers

You need --tree-filter:

git filter-branch \
    --tree-filter "find . -type f -not -wholename './source_code/*' -delete" HEAD
like image 70
vsminkov Avatar answered Nov 15 '22 04:11

vsminkov