Git submodule add: "a git directory is found locally" issue

GitGit Submodules

Git Problem Overview


I'm actually trying to learn how to use git, including the git submodule subcommands. I already set up a server on which I can host, push and pull git repositories by using SSH. I created a main git repository "Travail" on this server in which I would like to put all my projects as submodules.

In my Travail repository, I already added a project of mine as a submodule at tools/libft: I'm able to develop this submodule, to push and to pull it.

But when I try to add another submodule (named fdf, from fdf.git on my server), I get the following issue :

git submodule add ssh://XXX.XXX.XXX.XXX:XXXXX/opt/git/fdf.git projets/fdf

>A git directory for 'projets/fdf' is found locally with remote(s): origin ssh://[email protected]:XXXXX/opt/git/fdf.git If you want to reuse this local git directory instead of cloning again from ssh://XXX.XXX.XXX.XXX:XXXXX/opt/git/fdf.git use the '--force' option. If the local git directory is not the correct repo or you are unsure what this means choose another name with the '--name' option.

There is actually no subdirectory in projets/

I read on another thread that I should use git submodule sync or edit the .gitmodules file in which the URL to my submodule's origin repository could have changed.

But my .gitmodules file only contains the information about my first submodule (tools/libft), not about projets/fdf :

[submodule "tools/libft"]
	path = tools/libft
	url = ssh://[email protected]:XXXXX/opt/git/libft.git

As a French student I could have missed something in the English documentation I have, but I searched and I really don't understand why I get this issue.

I would be glad if I would get a solution but just an explanation would be helpful too.

Git Solutions


Solution 1 - Git

I came to this SO post trying to add a submodule with the same path as a submodule that I recently deleted.

This is what ultimately worked for me (this article helped out a lot):

If you haven't already run
git rm --cached path_to_submodule (no trailing slash) as well as
rm -rf path_to_submodule

Then:

  1. Delete the relevant lines from the .gitmodules file. e.g. delete these:
[submodule "path_to_submodule"]
	    path = path_to_submodule
	    url = https://github.com/path_to_submodule
  1. Delete the relevant section from .git/config. e.g. delete these:
[submodule "path_to_submodule"]
	    url = https://github.com/path_to_submodule
  1. rm -rf .git/modules/path_to_submodule

Then, you can finally:

git submodule add https://github.com/path_to_submodule

Solution 2 - Git

i tried jbmilgrom's solution, specifically i tried git rm --cache and that didn't work for me either as the directory/submodule wasn't there. What worked for me was:

  1. rm -rf .git/modules/blah
  2. git submodule add git://path.to.new

I did this after trying --force within the git submodule commands and rm all the other directories, pushes to master etc... the directory didn't exist and there was no reason for the cache. Turns out in .git/modules that is where this error was lying.

Solution 3 - Git

You may have deleted your 'projets/fdf' from disk, but your Git repository still has it. Use git rm -rf projets/fdf to remove it from Git, then commit the changes. After that you'll be able to add this folder as a submodule.

Solution 4 - Git

If you already deleted the submodule directory, like I did, follow the rest of jbmilgrom's instructions. The key is rm -rf .git/modules/path_to_submodule but go ahead and backup your whole parent repo directory first.

If you only had one submodule just delete .gitmodules

Solution 5 - Git

These two commands works for me.

rm path/to/submodule -rf
rm .git/modules/path/to/module -rf

Solution 6 - Git

You can do this while cloning your submodule:

git submodule add --name submodule_name submodule path

This will add your submodule into your project and will be added as a structure under .gitmodules file.

And then if you don't want the older one you can remove it from .gitmodules structure and delete the submodule folder from the project.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionvmontecoView Question on Stackoverflow
Solution 1 - GitjbmilgromView Answer on Stackoverflow
Solution 2 - GitpjammerView Answer on Stackoverflow
Solution 3 - GitbredikhinView Answer on Stackoverflow
Solution 4 - GitDmitri R117View Answer on Stackoverflow
Solution 5 - GitW.PerrinView Answer on Stackoverflow
Solution 6 - GitkeerthigaView Answer on Stackoverflow