使用git filter-branch删除除一个子目录外的所有内容

GDY*_*ell 5 git subdirectory

我一直在尝试使用git删除特定分支中除源代码之外的所有内容。的

git filter-branch -f \
    --subdirectory-filter source_code \
    --prune-empty \
    --tag-name-filter cat -- --all
Run Code Online (Sandbox Code Playgroud)

除了将文件放置source_code/在根目录而我希望它们全部保留在目录中之外,命令基本上可以为我提供所需的内容。即

- source_code
    - file1.py
    - file2.py
Run Code Online (Sandbox Code Playgroud)

不只是

- file1.py
- file2.py
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

vsm*_*kov 5

您需要--tree-filter

git filter-branch \
    --tree-filter "find . -type f -not -wholename './source_code/*' -delete" HEAD
Run Code Online (Sandbox Code Playgroud)