Search code examples
gitversion-controlgit-submodules

Why does manually changing .gitmodules not work?


I manually remove and add submodules by editing .gitmodules; however, executing git submodule update --init --recursive only clones moduleA which is existing before, my manually added submodule was not cloned. Running git submodule also shows old submodules that have been removed with error

fatal: no submodule mapping found in .gitmodules for path 'moduleOld'

while git submodule sync only shows moduleA again. Is there a way to fix this?


Solution

  • You shouldn't be manually adding/removing the submodule by editing the .gitmodules because git also tracks entries in the .git/config file. Try removing the submodule and committing in the temrinal. And then add the new submodule. To remove the submodules try:

    git rm --cached path/to/submodule   #updates Git to stop tracking the submodule
    rm -rf path/to/submodule            #removes the submodule files locally
    git commit -m 'Remove submodule'
    

    And then to add a new submodule try:

    git submodule add <repository> <path>
    git submodule update --init --recursive
    

    If you do make manually changes to the .gitmodules I believe running sync will synchronize the URL in the .git/config:

    git submodule sync
    git submodule update --init --recursive 
    

    This I think would update the URl but it doesn't add or remove submodule entries in .git/config.