276

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://git@XXX.XXX.XXX.XXX: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://git@XXX.XXX.XXX.XXX: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.

Mihai Chelaru
  • 6,705
  • 14
  • 41
  • 48
vmonteco
  • 12,434
  • 12
  • 50
  • 79
  • A small correction: it's 'repository', not 'depository'. Not really an answer, but if the projects are independent of each other it would be better for each project to have its own repo instead of submodules. – jcm Jan 05 '14 at 02:40
  • Thank you, I corrected. I know but I would like to gather all my work in the same repo but as separate projects. It is a choice to keep my whole work updated when I go from home to my school and vice versa (and to learn how to use these tools, I'm curious ;) ) – vmonteco Jan 05 '14 at 02:49
  • Well, curiosity is always a good reason! :) That is strange. Are you sure you haven't added the submodule before? – jcm Jan 05 '14 at 02:54
  • Not in Travail/, and I have no problème to clone fdf.git in ~/ . In Travail/ I find no traces of fdf. just libft :/ – vmonteco Jan 05 '14 at 03:11
  • 5
    Oh wait, I looked in `/Travail/.git/modules/projets/` and I found a `fdf` directory. This seems to not be in the working tree but in older commits. Could it generate the issue? – vmonteco Jan 05 '14 at 03:15
  • Yes, possibly. So it was committed in the main project? – jcm Jan 05 '14 at 03:43
  • related: http://stackoverflow.com/questions/12898278/issue-with-adding-common-code-as-git-submodule-already-exists-in-the-index/37336854#37336854 – Julio Marins May 20 '16 at 02:10
  • Possible duplicate of [How do I replace a git submodule with another repo?](http://stackoverflow.com/questions/14404704/how-do-i-replace-a-git-submodule-with-another-repo) – Chris Moschini Jul 01 '16 at 16:51

6 Answers6

688

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

ctf0
  • 6,444
  • 5
  • 34
  • 45
jbmilgrom
  • 15,815
  • 5
  • 23
  • 22
  • 91
    This was really helpful, as I could find nothing else that worked. The key difference from other suggestions was your step #3. Thanks! – AndroidDev May 14 '16 at 17:36
  • @Alex glad it was helpful! – jbmilgrom May 16 '16 at 17:04
  • 12
    Steps 2 & 3 were needed for me. – U007D Aug 17 '16 at 00:01
  • 12
    Step 3 was needed for me but, why does the submodule exist there too? – kgui Nov 07 '16 at 19:40
  • 1
    For me, I skipped steps 1 & 2 (which had the correct settings in it already) and step 3 fixed it for me. (I had already done the initial step of `git rm` and `rm -rf` steps.) – Rock Lee Dec 28 '16 at 22:05
  • Scratching my head for half hour and this (step 3) saved me! – Richard Fu Mar 15 '18 at 06:31
  • None of this worked for me. What ended up working was 'git submodule add' in a subdirectory, then git move submodule into place. Something went wrong somewhere in the names in caches I think. – mathtick Dec 20 '18 at 14:03
  • 5
    Even step 3 did not work for me. I had only one submodule so I deleted the entire `.git/modules` and that ended up working. – ach Apr 18 '19 at 14:35
  • 1
    Make sure your `submodule` directory is not in your `.gitignore` – Sudipta Basak Jul 16 '19 at 03:31
  • 1
    I had to remove `.git/modules/module_name` to get it working – taras Apr 13 '20 at 14:50
  • In my case I also had to close and open a new bash window, something was stored in memory! – Kim T Jul 04 '20 at 20:42
  • @jbmilgrom why `git rm --cached path_to_submodule`? I would `git rm` without `cached`. – Timo Aug 13 '20 at 17:51
  • This looks like a bug in git, because you can't solve it within `git` command scope, you will have to call `rm ./git/modules/path`, which is especially painful in a CI environment. And even locally, you have to do this every time you checkout between those branches. Is there any approach that can solve this within `git` command? something like parameter with `--no-keep-directory` that will clean `.git/modules/path/to/module`? – joe Sep 18 '20 at 02:32
  • Thank you! I was looking where those repository data stored in `.git` folder. And apparently, it was on `config` file and `modules` folder. New knowledge about git gained. – alramdein Oct 06 '20 at 02:00
  • 1
    Why is submodule support in git just so trash? – Henry Henrinson Mar 03 '21 at 12:26
  • Wow, what @#$%^@#%$ – Dan Nissenbaum Sep 08 '21 at 04:35
  • number 3 was really important ;) thank you – MehrAmoon Dec 09 '21 at 11:55
  • I did not know that submodule information is stored in `.git/modules/`... What a tricky mess! – FOR_SCIENCE Jun 02 '22 at 07:22
71

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.

pjammer
  • 9,227
  • 4
  • 44
  • 56
16

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.

bredikhin
  • 8,525
  • 3
  • 39
  • 44
  • It worked :) Thank you. I think I actually made this mistake. – vmonteco Jan 05 '14 at 04:21
  • I was searching a `git submodule rm` command, but there is no such command? isn't it? – vmonteco Jan 05 '14 at 05:35
  • @vmonteco No, there isn't. But you can use [this workflow](https://git.wiki.kernel.org/index.php/GitSubmoduleTutorial#Removal) or, for example, `git delete-submodule` command from [`git-extras`](https://github.com/visionmedia/git-extras) package by TJ Holowaychuk. – bredikhin Jan 05 '14 at 06:21
  • @vmonteco Also, it's a good practice in general to accept the right answer, so the other people could see it right away. ;) – bredikhin Jan 05 '14 at 07:06
  • That's right ;) But I didn't yet know how to do (it was my first question) Thanks for the explanations! – vmonteco Jan 05 '14 at 10:25
  • 22
    I get told the directory doesn't exist when I do this. Seems it both exists and doesn't :/ – Nathan Hornby Feb 06 '15 at 14:12
  • Same problem here, directory doesn't exists, can't git rm -rf it and can't add new submodule – sparklos Mar 15 '15 at 20:44
  • @sparklos Did you figure this out? I'm dealing with the same problem – Tom McFarlin Mar 30 '15 at 00:14
  • 43
    @tom-mcfarlin In the end, Imanually removed lines from .gitmodule and a folder from .git/modules, then it let me add a new submodule. Maybe there's better way to do this, but I was running out of time. If you gonna go this way, I recommend you make a good backup and please don't blame me, if it goes wrong for you. Good luck. – sparklos Mar 30 '15 at 10:43
  • 5
    Thank you, I found on another post to remove the folder from .git/modules and that worked for me. Makes sense as that's what the error said was the conflict... I just was unaware of that folder – Tom McFarlin Mar 30 '15 at 15:10
  • 10
    @NathanHornby @Sparklos @TomMcFarlin -- In more recent versions of git, you will find that git-submodule adds a directory in .git folder. For example, if you are in directory `/tmp/repo`, then run `git submodule add ../otherrepo.git`, then you will see that `/tmp/repo/.git/modules/otherrepo` folder exists. That is what the problem was for me. I manually deleted that folder from .git, and everything worked. – Alexander Bird Mar 04 '16 at 20:40
  • This isn't the correct answer. It doesn't remove the submodule completely. The answer by @jbmilgrom coveres the problem in detail. – Janos May 12 '17 at 08:20
16

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

Dmitri R117
  • 1,912
  • 20
  • 14
9

These two commands works for me.

rm path/to/submodule -rf
rm .git/modules/path/to/module -rf
W.Perrin
  • 3,181
  • 29
  • 28
  • This. It seems there are two ways for submodules to be created. In one of them, the subfolder will only have a single .git file that simply points to a directory in the superproject, starting with `.git/modules/`. In there is a folder that stores the repository for the submodule. Deleting that repository fixed the issue for me, while the accepted answer didn't. – William Randokun Mar 18 '19 at 10:29
0

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.

aalbagarcia
  • 989
  • 6
  • 19
keerthiga
  • 1
  • 1