Sunday, September 26, 2021

How to Split a Subdirectory to a New Git Repository and Keep the History

 

(modified from: https://ao.ms/how-to-split-a-subdirectory-to-a-new-git-repository-and-keep-the-history/)


Say, original local path is ~/allMyCode

1) origin repo to check out the branch with all the subdirectory history, eg. master


    git checkout master


2) clone a copy


    git clone  ~/allMyCode  ~/newSubset

    cd ~/newSubset


3) in new clone, remove all but the sub directory (below is a one-liner)


    git filter-branch --prune-empty --subdirectory-filter relative/path/to/subdirectory current_branch_name


4) if the new clone is a branch other than 'master', make it the new 'master'


    git branch -m master


5) remove untracked leftovers, such as ignored/hidden files/folders (WARNING: make sure you have backup)


    git clean -fdx


6) fix remote repo URL    // assume new repo empty


    git remote set-url origin  {newRepoURL}


7) push to new repo


    git push -u origin master



Labels: , , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home