3

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?

sha
  • 17,824
  • 5
  • 63
  • 98
  • Possible duplicate of [How can I move a single directory from a git repository to a new repository whilst maintaining the history?](https://stackoverflow.com/questions/811251/how-can-i-move-a-single-directory-from-a-git-repository-to-a-new-repository-whil) – phd Nov 02 '17 at 20:14
  • One you referring to is actually very brief. I looked at https://stackoverflow.com/questions/359424/detach-move-subdirectory-into-separate-git-repository/17864475#17864475 and found most information there. But neither one of these are exact my issue. – sha Nov 02 '17 at 20:42
  • Does this answer your question? [git splitting repository by subfolder and retain all old branches](https://stackoverflow.com/questions/20757601/git-splitting-repository-by-subfolder-and-retain-all-old-branches) – SpeziFish Feb 12 '20 at 10:51

1 Answers1

12

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
sha
  • 17,824
  • 5
  • 63
  • 98