sha
sha

Reputation: 17850

Split one subfolder from git repository with all branches and history

I would like to split on folder from my git repository into new one. But I would like to keep all open/closed branches and history for everything related to this folder.

\ Source Repo
--\ AAA
--\ BBB
----\ DDD
----\ EEE
--\ CCC


\ New Repo 1
--\ AAA
--\ CCC

\ New Repo 2 (BBB subfolder)
--\ DDD
--\ EEE

I followed steps described here https://help.github.com/articles/splitting-a-subfolder-out-into-a-new-repository/ - but this work only for a single branch. At the end - I get new repository with all (?) commits but no branch information is present.

I tried to do this for all branches using --all parameter, but I'm not clear how to push all rewritten branches to the empty remote repo.

Here is what I have currently:

$ git clone https://github.com/USERNAME/REPOSITORY-NAME
$ cd REPOSITORY-NAME
$ git filter-branch --prune-empty --subdirectory-filter FOLDER-NAME -- --all

At this point if I look at my local repo, I seem to have all history and all branches in remotes origins.

$ git remote -v
origin  https://github.com/USERNAME/REPOSITORY-NAME.git (fetch)
origin  https://github.com/USERNAME/REPOSITORY-NAME.git (push)
$ git remote set-url origin https://github.com/USERNAME/NEW-REPOSITORY-NAME.git
$ git remote -v
origin  https://github.com/USERNAME/NEW-REPOSITORY-NAME.git (fetch)
origin  https://github.com/USERNAME/NEW-REPOSITORY-NAME.git (push)


$ git push -u origin --all 

Last command doesn't seem to be pushing all branches. What am I missing?

Upvotes: 3

Views: 3001

Answers (1)

sha
sha

Reputation: 17850

Found missing pieces of information here Detach (move) subdirectory into separate Git repository

Finished scripts looks like this. Use this process for both new repositories with one line being different

Clone source repository:

$ git clone https://github.com/USERNAME/REPOSITORY-NAME
$ cd REPOSITORY-NAME

Re-create all remote branches locally

$ for i in $(git branch -r | sed "s/.*origin\///"); do git branch -t $i origin/$i; done

Use filter-branch to trim everything for required subfolder (this will create new repo using only FOLDER-NAME

$ git filter-branch --prune-empty --subdirectory-filter FOLDER-NAME -- --all

Use filter-branch to remove FOLDER-NAME (this will create new repository with everything but FOLDER-NAME)

$ git filter-branch --prune-empty --tree-filter 'rm -rf FOLDER-NAME' -- --all

Update remote URL to new empty repo:

$ git remote -v
origin  https://github.com/USERNAME/REPOSITORY-NAME.git (fetch)
origin  https://github.com/USERNAME/REPOSITORY-NAME.git (push)
$ git remote set-url origin https://github.com/USERNAME/NEW-REPOSITORY-NAME.git
$ git remote -v
origin  https://github.com/USERNAME/NEW-REPOSITORY-NAME.git (fetch)
origin  https://github.com/USERNAME/NEW-REPOSITORY-NAME.git (push)

Push everything together

$ git push -u origin --all

Upvotes: 12

Related Questions