Skip to content

Instantly share code, notes, and snippets.

@BernhardBaumrock
Forked from myusuf3/delete_git_submodule.md
Last active May 21, 2022 12:33
Show Gist options
  • Save BernhardBaumrock/518a5fae323118548e90f6b7db95cc81 to your computer and use it in GitHub Desktop.
Save BernhardBaumrock/518a5fae323118548e90f6b7db95cc81 to your computer and use it in GitHub Desktop.
git remove submodule

To remove a submodule you need to:

  • Delete the relevant section from the .gitmodules file.
  • Stage the .gitmodules changes git add .gitmodules
  • Delete the relevant section from .git/config.
  • Run git rm --cached path_to_submodule (no trailing slash).
  • Run rm -rf .git/modules/path_to_submodule (no trailing slash).
  • Commit git commit -m "Removed submodule "
  • Delete the now untracked submodule files rm -rf path_to_submodule
@BernhardBaumrock
Copy link
Author

BernhardBaumrock commented Feb 14, 2022

# Remove the submodule entry from .git/config
git submodule deinit -f path/to/submodule

# Remove the submodule directory from the superproject's .git/modules directory
rm -rf .git/modules/path/to/submodule

# Remove the entry in .gitmodules and remove the submodule directory located at path/to/submodule
git rm -rf path/to/submodule

@BernhardBaumrock
Copy link
Author

BernhardBaumrock commented Mar 11, 2022

git rm <path-to-submodule>
# commit
rm -rf .git/modules/<path-to-submodule>
git config --remove-section submodule.<path-to-submodule>.

In modern git (I'm writing this in 2022, with an updated git installation), this has become quite a bit simpler:

Run git rm , and commit.
This removes the filetree at , and the submodule's entry in the .gitmodules file. I.e. all traces of the submodule in your repository proper are removed.

As the docs note however, the .git dir of the submodule is kept around (in the modules/ directory of the main project's .git dir), "to make it possible to checkout past commits without requiring fetching from another repository".
If you nonetheless want to remove this info, manually delete the submodule's directory in .git/modules/, and remove the submodule's entry in the file .git/config. These steps can be automated using the commands

rm -rf .git/modules/, and
git config --remove-section submodule..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment