Search code examples
gitgit-submodules

Submodule URL Change still gives old files


I have a submodule added to my repsitory to a specific local path with a URL to Github. Now I removed that submodule and added another submodule with the same path and name to another Github URL (a fork from the first URL with some changes). The submodule path still shows the files the from the original URL.

What's wrong? Did I miss some commands to get that clean?

The .gitmodules file is empty after removing the submodule and after adding the new submodule the new URL is in the file. So that file is not the problem.

git submodule sync does not work

I haven't found anything helpful in other articles either

What am I missing?


Solution

  • When you add a submodule it adds a git objects directory for that submodule under the parent project's .git directory.

    git init
    mkdir -p path/to/my
    git submodule add https://example.com/foo.git path/to/my/foomodule
    # Repo files for foo.git are now located in .git/modules/path/to/my/foomodule
    

    Even if you git rm the path from your working directory, the submodule's directory inside .git will stick around.

    At this point you can safely delete .git/modules/path/to/my/foomodule, and you probably should; if you attempt to add a different submodule at that path, git will attempt to re-use those repo files which is leading to what you're seeing.

    AFAIK there's no built-in git porcelain command to clean out unused submodule directories, so you're likely stuck using rm -rf.