Search code examples
gitpowershellatlassian-sourcetree

How to create submodule in existing repo


I'm beginner to git & I have the following folder structure for new project, so what I did is I went to visual studio and I made empty project it has this folder structure:

project: (Repo) 
folder1 (sub1)
folder2 (sub2)

I'm using git locally.
I created a repo for project using sourcetree,
now I cannot add any submodule, I don't know why.

In powershell whenever I try:

PS E:\Projects\Project> git submodule add ./sub1

I get the following error:

sub1 already exists in the index

then I decided to remove sub1 using git rm -r sub1,and add it again, then when I tried to add it again using git submodule add again, I received this new error:

sub1 already exists and is not a valid git repo

So what I'm doing wrong?


Solution

  • Adding a submodule means cloning another git repo inside an existing one and keep a reference to it.

    In your case, you did not specify which repo you want to add:

    git submodule add -- /url/of/sub1/repo sub1
    

    if sub1 or sub2 are not supposed to be git repos of their own, but simple sub-folder of the main repo folder, then you do not need git submodule add command.
    Simply add files in sub1 and sub2, and you will be able to git add and git commit them.
    No submodule involved.