5

I have a git repository in the parent folder. Lets call this folder "root". Then there is a sub directory in the folder, lets call it "child". What I want to do is clone a remote repository as a submodule into "child":

git submodule add git@github.com:username/repopath child/
git submodule add git@github.com:username/repopath ./child/

Both the above give me the error:

child already exists in the index

I tried removing the directory from being tracked:

git rm --cached .\child\*
git submodule add git@github.com:username/repopath child/

Then i get this error:

'child' already exists and is not a valid git repo

Any help would be most appreciated

Thanks

pedrumj
  • 163
  • 2
  • 12

1 Answers1

6

First, try your submodule add command in a new clone, because your previous attempts might have left a partial state for submodule.

Second, try first

git rm -r --cached child

Then, add and commit.

Finally, try your git submodule command:

git submodule add -- git@github.com:username/repopath child

Add, and commit.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 2
    For learners, `git submodule add -- git@github.com:username/repopath child`. `--`: after the two dashes, there are only folders and files, no options. `git@githu...` is a `ssh` address, not `https`. – Timo Aug 13 '20 at 18:05
  • 2
    @Timo For more on the double hyphen (or double dash): https://stackoverflow.com/a/1192194/6309 – VonC Aug 13 '20 at 21:45
  • 1
    Huge thanks to @VonC for your answer and comment. Very useful – Son Nguyen Apr 30 '23 at 18:15