How to change a tracked folder into a git submodule
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?
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:
- Initialize a new repository in the folder that will later be a submodule;
- Commit the contents to a remote;
- Remove this folder from the main project with git rm -rf <folder_that_will_be_a_submodule>;
- Now do the same but with rm -rf;
- Go to your project root (where the main .git lives);
- Execute git submodule add <repository>;
- 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.