Rui Gonçalves Rui Gonçalves - 3 months ago 15
Git Question

Add a submodule which can't be removed from the index

I'm trying to add a submodule that already existed (different git remote repository). As I didn't searched before how to do it correctly, I think I've messed up my repository and I need some help to fix it again.

I've already deleted all the relevant sections from the .gitmodules and .git/config regarding the submodules I want to delete. I've also verified that there is not modules directory inside my .git/ directory.

However, when I run the command

git rm --cached path_to_submodule
, the following message is displayed:


fatal: pathspec 'path_to_submodule' did not match any files


As the previous command fails, when I try to add again the same submodule with the new definitions, running the command
git submodule add gituser@host:repo.git
, this is the displayed message:


'repo' already exists in the index

Answer

The only way that message ('repo' already exists in the index) can be displayed is if 'repo' still exists in the index (see this chapter on submodule):

$ rm -Rf rack/
$ git submodule add git@github.com:schacon/rack.git rack
'rack' already exists in the index

You have to unstage the rack directory first. Then you can add the submodule:

$ git rm -r rack
$ git submodule add git@github.com:schacon/rack.git rack

Even if 'rack' isn't a submodule, if it exists, it would prevent the declaration of a submodule of the same name.