Delete all but specific folders from git history

I have a complex git repository from which I would like to delete ALL files and history, except for two folders, let's say:

foo/a
bar/x/y

While git filter-branch --subdirectory-filterit allows me to select one folder and make it new, it does not seem to give me a choice to select two directories and save their location.

git filter-branch --tree-filteror --index-filterit seems like it will allow me to iterate over every commit in the history where I can use git rmin an unwanted folder.

I cannot find any working way to get these commands to simply save the two folders that I need, clearing everything .

Thank!

+4
source share
2 answers

: git filter-branch.

, ( 10 - 100 ). - , , , ( ). , , . , , . ( - , Git .) , , A B, , , - , A, B:

find . -name A -prune -o -name B -prune -o -print0 | xargs -0 rm

.

, , Git , , . , git rm -rf --cached --ignore-unmatch, , git update-index . Git, . Unix find.

, , git ls-files, . , , ( Python, , Perl), :

for (all files in the index)
    if (file name starts with 'A/' or 'B/')
        do nothing
    else
        add to removal list
invoke "git rm --cached" on paths in removal list

, , :

git ls-files | IFS=$'\n' while read path; do
    case "$path" in A/*|B/*) continue;; esac
    git rm --cached "$path"
done

( git rm --cached !), " " --index-filter.

(, , , : pipe git ls-files grep -v , pipe grep git update-index --force-remove --stdin. . )

+1

git fast-export. , . git fast-export find.

git fast-export HEAD -- `find foo/a bar/x/y -type f` >../myfiles.fi

.

 git init
 git fast-import <../myfiles.fi
0

Source: https://habr.com/ru/post/1672464/


All Articles