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.

  • How do I use 'git rebase -i' to rebase all changes in a branch?
  • How to share git repo between Mac and Windows running on VMWare?
  • How rebase result may differ from result of a merge?
  • git diffrence between clone repo and clone rep.git
  • How to migrate gitorious to gitlab repositories
  • git error: There are still logs under
  • - source_code
        - file1.py
        - file2.py
    

    not just

    - file1.py
    - file2.py
    

    How can I do this?

  • git clone hangs at “checking connectivity”
  • github pages site can't find content
  • How do you remove files from one and only one branch in git
  • Git pull error: unable to create file (Invalid argument)
  • git - can't push to remote, error 'fatal: Not a git repository'
  • Why does Git ignore the .vs directory?
  • One Solution collect form web for “Using git filter-branch to remove everything except one subdirectory”

    You need –tree-filter:

    git filter-branch \
        --tree-filter "find . -type f -not -wholename './source_code/*' -delete" HEAD
    
    Git Baby is a git and github fan, let's start git clone.