How to split a git repository while preserving subdirectories?

后端 未结 7 1587
轻奢々
轻奢々 2020-11-27 16:11

What I want is similar to this question. However, I want the directory that is split into a separate repo to remain a subdirectory in that repo:

I have this:

7条回答
  •  清歌不尽
    2020-11-27 16:17

    This is what I ended up doing to solve this issue when I had it myself:

    git filter-branch --index-filter \
    'git ls-tree --name-only --full-tree $GIT_COMMIT | \
     grep -v "^directory-to-keep$" | \
     sed -e "s/^/\"/g" -e "s/$/\"/g" | \
     xargs git rm --cached -r -f --ignore-unmatch \
    ' \
    --prune-empty -- --all
    

    The solution is based on Jefromi’s answer and on Detach (move) subdirectory into separate Git repository plus many comments here on SO.

    The reason why Jefromi’s solution did not work for me was, that I had files and folders in my repo whose names contained special characters (mostly spaces). Additionally git rm complained about unmatched files (resolved with --ignore-unmatch).

    You can keep the filtering agnostic to the directory not being in the repo’s root or being moved around:

    grep --invert-match "^.*directory-to-keep$"
    

    And finally, you can use this to filter out a fixed subset of files or directories:

    egrep --invert-match "^(.*file-or-directory-to-keep-1$|.*file-or-directory-to-keep-2$|…)"
    

    To clean up afterwards you can use these commands:

    $ git reset --hard
    $ git show-ref refs/original/* --hash | xargs -n 1 git update-ref -d
    $ git reflog expire --expire=now --all
    $ git gc --aggressive --prune=now
    

提交回复
热议问题