How do I manage files that are in the path of a previous Git submodule?

If you previously used a Git submodule to pull in third-party code and now want to manage that directory as a regular part of your repository, you need to fully remove the submodule first. Simply deleting the folder or running git add on it will not work because Git still has submodule metadata referencing that path.

Remove the submodule reference from the index

Start by telling Git to stop tracking the submodule:

$ git rm --cached path/to/submodule
rm 'path/to/submodule'

This removes the submodule entry from Git's index but leaves the files on disk.

Remove the submodule from .gitmodules

Open .gitmodules and delete the section for your submodule:

[submodule "path/to/submodule"]
    path = path/to/submodule
    url = https://github.com/example/library.git

Remove the entire block, then stage the change:

$ git add .gitmodules

If this was the only submodule, you can remove the .gitmodules file entirely.

Remove the submodule from .git/config

Open .git/config and delete the matching section:

[submodule "path/to/submodule"]
    url = https://github.com/example/library.git

Clean up the submodule directory

Git keeps internal submodule data in .git/modules/. Remove it:

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

If the actual submodule directory is still on disk from the earlier step, make sure it contains the files you want to keep. If it is empty or stale, delete and re-create it with the files you need.

Add your files to the repository

Now Git no longer thinks that path is a submodule. You can add files there normally:

$ git add path/to/submodule/

Git will treat these as regular tracked files from now on.

Commit the changes

Bundle everything into a single commit:

$ git commit -m "Replace submodule with local files"
$ git push

Your collaborators will get the change cleanly on their next pull. The directory is now a standard part of the repository.

For more on submodule management, see the git submodule reference. Related commands: git add and git commit.


DeployHQ handles submodule checkouts during deployment automatically. Try it free.