If the submodule was accidentally added because you added, committed and pushed a folder that was already a Git repository (contained .git), you won’t have a .gitmodules file to edit, or anything in .git/config. In this case all you need is :
The majority of answers to this question are outdated, incomplete, or unnecessarily complex.
A submodule cloned using git 1.7.8 or newer will leave at most four traces of itself in your local repo. The process for removing those four traces is given by the three commands below:
# 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 -f path/to/submodule
In theory, git rm in step 1 should take care of it. Hopefully, the second part of OP question can be answered positively one day (that this can be done in one command).
But as of July 2017, step 2 is necessary to remove data in .git/modules/ for otherwise, you can't e.g. add the submodule back in the future.
You can probably get away with the above two steps for git 1.8.5+ as tinlyx's answer noted, as all git submodule commands seem to work.
Step 3 removes the section for the_submodule in the file .git/config. This should be done for completeness. (The entry may cause problems for older git versions, but I don't have one to test).
For this, most answers suggest using git submodule deinit. I find it more explicit and less confusing to use git config -f .git/config --remove-section. According to the git-submodule documentation, git deinit:
Unregister the given submodules ... If you really want to remove a
submodule from the repository and commit that use git-rm[1]
instead.
Last but not least, if you don't git commit, you will/may get an error when doing git submodule summary (as of git 2.7):
fatal: Not a git repository: 'the_submodule/.git'
* the_submodule 73f0d1d...0000000:
This is regardless of whether you do steps 2 or 3.
If the submodule was accidentally added because you added, committed and pushed a folder that was already a Git repository (contained
.git
), you won’t have a.gitmodules
file to edit, or anything in.git/config
. In this case all you need is :FWIW, I also removed the
.git
folder before doing thegit add
.Via the page Git Submodule Tutorial:
To remove a submodule you need to:
.gitmodules
file..gitmodules
changesgit add .gitmodules
.git/config
.git rm --cached path_to_submodule
(no trailing slash).rm -rf .git/modules/path_to_submodule
git commit -m "Removed submodule <name>"
rm -rf path_to_submodule
See also: alternative steps below.
The majority of answers to this question are outdated, incomplete, or unnecessarily complex.
A submodule cloned using git 1.7.8 or newer will leave at most four traces of itself in your local repo. The process for removing those four traces is given by the three commands below:
To summarize, this is what you should do :
Set
path_to_submodule
var (no trailing slash):path_to_submodule=path/to/submodule
Delete the relevant line from the .gitmodules file:
git config -f .gitmodules --remove-section submodule.$path_to_submodule
Delete the relevant section from .git/config
git config -f .git/config --remove-section submodule.$path_to_submodule
Unstage and remove $path_to_submodule only from the index (to prevent losing information)
git rm --cached $path_to_submodule
Track changes made to .gitmodules
git add .gitmodules
Commit the superproject
git commit -m "Remove submodule submodule_name"
Delete the now untracked submodule files
rm -rf $path_to_submodule
rm -rf .git/modules/$path_to_submodule
Here are the 4 steps that I found necessary or useful (important ones first):
In theory,
git rm
in step 1 should take care of it. Hopefully, the second part of OP question can be answered positively one day (that this can be done in one command).But as of July 2017, step 2 is necessary to remove data in
.git/modules/
for otherwise, you can't e.g. add the submodule back in the future.You can probably get away with the above two steps for git 1.8.5+ as tinlyx's answer noted, as all
git submodule
commands seem to work.Step 3 removes the section for
the_submodule
in the file.git/config
. This should be done for completeness. (The entry may cause problems for older git versions, but I don't have one to test).For this, most answers suggest using
git submodule deinit
. I find it more explicit and less confusing to usegit config -f .git/config --remove-section
. According to the git-submodule documentation,git deinit
:Last but not least, if you don't
git commit
, you will/may get an error when doinggit submodule summary
(as of git 2.7):This is regardless of whether you do steps 2 or 3.
You can use an alias to automate the solutions provided by others:
Put that in your git config, and then you can do:
git rms path/to/submodule