L
L
lexnekr2014-07-10 13:17:50
git
lexnekr, 2014-07-10 13:17:50

How to submodule an existing part of a GIT repository?

How to connect an external repo as a submodule to an existing repository is more or less clear:

$ git submodule add [email protected]:schacon/rack.git rack

But what if, in the course of the life of the project, it was suddenly decided to allocate part of the functionality into a submodule?
For example, the entire project is under version control, including the /sport/soccer/ section.
How can I make this existing section a repository (submodule)?
git submodule add [email protected]:schacon/rack.git sport/soccer will not work (it will give an error, justifying that this section is already in the version control system).
I don't want to delete the partition, commit without it and connect the external repo in order to restore the partition.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Entelis, 2014-07-10
@lexnekr

I don't want to delete the partition, commit without it and connect the external repo in order to restore the partition.

And yet it's the only way

I
i, 2014-09-13
@ilyarsoftware

As @DmitriyEntelis wrote "delete the partition, commit without it, and link the external repo in order to restore the partition." this is the only way, but if we want to save the history of the selected part of the project, we can do the following:

git clone <git repository A url>
cd <git repository A directory>
git remote rm origin
git filter-branch --subdirectory-filter <directory 1> -- --all
mkdir <directory 1>
mv * <directory 1>
git add .
git commit

Source: Moving Files from one Git Repository to Another, P...

B
bl, 2018-07-05
@vl12

Another option:
Select a directory in a branch via `git subtree split`, load history into it, disable tracking from the old repository. More.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question