31

I have this Git repository which contains two folders: binary-search and poker.

For example: https://github.com/soulnafein/code-katas

I would like to turn these folders into submodules and keep their change history.

How can I do that?

Matthias Braun
34.7k27 gold badges158 silver badges177 bronze badges
asked Oct 20, 2009 at 10:10
2

2 Answers 2

15

Today there's a better way of doing this: git subtree

See this answer.

answered May 14, 2012 at 11:36
1
13

The general idea is to use 'git filter-branch' and the following steps:

1) Create a submodule using --subdirectory-filter of filter-branch (after cloning your repo).

$ git filter-branch --subdirectory-filter ABC HEAD -- --all

See this SO question for more on this step.

2) Create a superproject using an index filter of filter-branch to delete the submodule.

$ git filter-branch --index-filter "git rm -r -f --cached --ignore-unmatch ABC" --prune-empty HEAD

3) Commit the submodule to the latest version of the superproject.

See Detach subdirectory into separate git repository for a practical example.

Each submodule will keep its history.
But as said in this patch proposal, it would:

lose all the historical connections between the superproject and the submodule, breaking tools like 'git bisect', and making it difficult to recover old releases.

Ideally, each version of the newly created superproject would be linked to the correct version of the submodule (and all the .gitmodules entries would be set up correctly, too, throughout the project's history)

If you do not need to have previous history linked to the new submodules, you can follow the steps mentioned above.
But if you do need to branch from an older point while have references to your submodules (which are currently simple sub-directories), the you could consider trying the script mentioned in the patch I refer to. It is discussed in this thread, but integrated to Git yet, as Junio C Hamano says:

Unfortunately, I do not think we have designed fully (nor implemented at all) behaviour to check out different points of history that has the same submodule moved around in the superproject tree.

answered Oct 20, 2009 at 10:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.