How to change a tracked folder into a git submodule

206 Views Asked by At

I'd like to change one folder in my project into a submodule. This is my (sumplified) directory structure:

|-app <- the root of git project   
|--themes
|--plugins
|--lib

Now, I want to turn app/plugins into a submodule so that I commit changes in this directory into a separate reposiotry.

After setting up a new repo for plugins, I've tired git submodule add <submodule-repository> plugins, but then I receive a message the reads

app/plugins' already exists in the index

Sure it exists in the index since it was tracked as part of the main project repository.

How should I solve it? Should I perhaps delete the main .git folder, add app/plugins to .gitignore, commit again, and then run git submodule add?

This actually raises another question, should a folder that is a submodule of the project be added to .gitignore of the project that includes the submodule?

1

There are 1 best solutions below

1
On

I managed to answer my own question. Perhaps I posted too soon. Nevertheless maybe it will help some people in the future:

In order to turn a folder tracked in the current project into this project's submodule, I took the following steps:

  1. Initialize a new repository in the folder that will later be a submodule;
  2. Commit the contents to a remote;
  3. Remove this folder from the main project with git rm -rf <folder_that_will_be_a_submodule>;
  4. Now do the same but with rm -rf;
  5. Go to your project root (where the main .git lives);
  6. Execute git submodule add <repository>;
  7. Remove references to the folder that is now a submodule form your main .gitignore (if it has been referenced there).

Hope it will help somebody.