How to make part of an existing Git repository a submodule

后端 未结 2 601
小蘑菇
小蘑菇 2020-12-23 21:02

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

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

相关标签:
2条回答
  • 2020-12-23 21:28

    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.

    0 讨论(0)
  • 2020-12-23 21:39

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

    See this answer.

    0 讨论(0)
提交回复
热议问题