1

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?

2
  • You could just delete it from the repo? Commented Apr 26, 2018 at 21:14
  • @evolutionxbox, will do if there is no better solution. Commented Apr 26, 2018 at 22:38

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Only disadvantage: /lib now points only to single library repo (submodule), not sure if I can add other libraries there.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.