Splitting the repository

Below are the instructions of how to split an existing repository based on folder structure into two, or more. Let us assume the repository has dir1 and dir2 in the top level folder structure. The following instructions will make a new repo based on dir1.


    # make a copy of the original repo
    git clone yourRemote dir1
    cd dir1
    # move to the headless state
    git checkout --detach
    # delete all branches
    git branch | grep --invert-match "*" | xargs git branch -D
    # check what remote branches you want to keep
    git branch --remotes
    # recreate all local branches
    git checkout --track yourRemote/branch1
    git checkout --track yourRemote/branch2

    # remove all remotes and remote branches
    git remote remove origin
    # isolate dir1 and recreate branches
    git filter-branch --prune-empty --subdirectory-filter dir1 -- --all
    # add new remote, push your new repo, and you're done
    git remote add yourNewRemote
    git push --all



Deleting loose objects

    rm -rf .git/refs/original/*
    git reflog expire --all --expire-unreachable=0
    git repack -A -d
    git prune    (git gc --prune=now)

    # And, if it happens that you have some stale remote branches
    # removing stale remote branch
    git branch -d -r staleremote/master



Leave a Reply

Your email address will not be published. Required fields are marked *