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

6

35

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
Semiprofessional answered 31/8, 2012 at 15:29 Comment(1)
I was trying to add a submodule with the same name of an already existing file in the index.Sinuous
G
72

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 [email protected]: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 [email protected]: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.

Godspeed answered 31/8, 2012 at 16:7 Comment(0)
D
14

If the output adding a new submodule is:

'FolderName' already exists in the index

Tip the next commands

git ls-files --stage 

The output will be something similar to:

160000 d023657a21c1bf05d0eeaac6218eb5cca8520d16  0  FolderName

Then, to remove the folder index tip:

git rm -r --cached FolderName

Try again add the submodule

Devilfish answered 26/9, 2016 at 14:37 Comment(1)
"git rm -r --cached" is the key to fix the conflict issue for the existing submodule already registered in the index tree but couldn't be gotten thru git add that submodulePlantain
R
4

May occur, when merging with error, manual deleting of folder of submodule, or something else, like Hallileo Comet

  1. in file .gitmodules - delete links to submodule (whole section with submodule name)

  2. in file .git\config - delete links to submodule, as in previous step

  3. in folder .git\modules - delete folder with relative path similar to relative path of "problem" module

  4. ensure, that folder of submodule is not exists anymore

  5. then:

    $ git submodule add -f --name <name> <git://path_1.git> <path_2>

    where: name - name of submodule as u wish, may be equal your repo name; - path to submodule source repo (ie - github, etc), - relative path to folder where submodule will reside

    this lets u to add submodule within path or with name which still present in index, but not naturally alive.

i didn't found any method to remove these dead links from index, but when forced

Ratcliffe answered 28/8, 2016 at 9:3 Comment(0)
A
1

'submodules/uasdk-clib' already exists in the index

git rm -r --cached submodules/uasdk-clib

git submodule add -b china/release/16.8.0 -f ssh://[email protected] submodules/uasdk-clib

Amazed answered 29/12, 2016 at 4:14 Comment(0)
V
1

this is because you have the folder in your repo same name as the name of your submodule

$ git rm -r subModuleName
$ git submodule add "your submodule repo path without these quotes"

Try again adding the submodule

Vagus answered 16/10, 2019 at 7:54 Comment(0)
S
0

If you have otherwise completely removed a submodule, and are trying to add it back in again, but you're getting an error that it already exists in the index......

git update-index --remove path_to_submodule

...to remove it from the index. You should then be able to add the submodule again.

Sportscast answered 11/11, 2023 at 4:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.