Aleksey Kontsevich
Aleksey Kontsevich

Reputation: 5011

Move a subfolder out into a new repository

I need to move a subfolder out into a new git repository and then re-add it as submodule to the original repo and preserve original directory structure. Github help suggest to use

git filter-branch --prune-empty --subdirectory-filter FOLDER-NAME  BRANCH-NAME 

to filter original repo which preserves history related to this folder, etc. However this approach is not complete, good to filter out this dir and its history from original repo and re-add it as submodule at the point in history directory was created.

I have following directory structure:

/
  /lib
      /mylib
  /server
  /src
  /tests
  LICENSE
  README.md

Command

git filter-branch --prune-empty --subdirectory-filter lib/mylib master

moves sources to the root of new repo which is not what I want. Same for upper directory:

git filter-branch --prune-empty --subdirectory-filter lib master

keeps mylib dir in filtered repo (but potentially could grab other libraries - not my case for lucky) and work a little bit better. So what are next steps:

  1. Filter out /lib/mylib and its history from original repo to reduce repo size and improve its structure (simple way just remove it and re-add as submodule).
  2. Re-add /lib/mylib repo as submodule to preserve directory structure
  3. Good to re-add 2nd on the point in history when /lib/mylib was created to keep repo consistent and sources build-able on any stage (or it has no sense?).

Hard to do? Any thoughts?

Upvotes: 1

Views: 262

Answers (1)

Aleksey Kontsevich
Aleksey Kontsevich

Reputation: 5011

To keep same paths did the following:

  1. git filter-branch --prune-empty --subdirectory-filter lib master
  2. Changed remote URL for filtered repository like mentioned in Github manual.
  3. Pushed filtered repo to the new origin
  4. Made sure lib dir was removed in primary local repo with: git rm -r mylib
  5. Re-add lib as submodule: git submodule add https://origin_repo/MyLib.git lib

Upvotes: 1

Related Questions