After experimenting with all the different answers on this site, I ended up with this solution:
#!/bin/sh
path="$1"
if [ ! -f "$path/.git" ]; then
echo "$path is no valid git submodule"
exit 1
fi
git submodule deinit -f $path &&
git rm --cached $path &&
rm -rf .git/modules/$path &&
rm -rf $path &&
git reset HEAD .gitmodules &&
git config -f .gitmodules --remove-section submodule.$path
This restores the exact same state as before you added the submodule. You can right away add the submodule again, which was not possible with most of the answers here.
git submodule add $giturl test
aboveScript test
This leaves you with a clean checkout with no changes to commit.
Then things are done. The submodule directory will be removed from your repo and still exist in your filesystem. You can then commit the change like: git commit -am "Remove the submodule".
What I'm currently doing Dec 2012 (combines most of these answers):
After experimenting with all the different answers on this site, I ended up with this solution:
This restores the exact same state as before you added the submodule. You can right away add the submodule again, which was not possible with most of the answers here.
This leaves you with a clean checkout with no changes to commit.
This was tested with:
I had to take John Douthat's steps one step further and
cd
into the submodule's directory, and then remove the Git repository:Then I could commit the files as a part of the parent Git repository without the old reference to a submodule.
Simple steps
git config -f .git/config --remove-section submodule.$submodulename
git config -f .gitmodules --remove-section submodule.$submodulename
git rm --cached $submodulepath
rm -rf $submodulepath
rm -rf .git/modules/$submodulename
Please note:
$submodulepath
doesn't contain leading or trailing slashes.Background
When you do
git submodule add
, it only adds it to.gitmodules
, but once you didgit submodule init
, it added to.git/config
.So if you wish to remove the modules, but be able to restore it quickly, then do just this:
It is a good idea to do
git rebase HEAD
first andgit commit
at the end, if you put this in a script.Also have a look at an answer to Can I unpopulate a Git submodule?.
I recently find out a git project which include many useful git related command: https://github.com/visionmedia/git-extras
Install it and type :
Then things are done. The submodule directory will be removed from your repo and still exist in your filesystem. You can then commit the change like:
git commit -am "Remove the submodule"
.Here is what I did :
1.) Delete the relevant section from the .gitmodules file. You can use below command:
2.) Stage the
.gitmodules
changes3.) Delete the relevant section from
.git/config
. You can use below command:4.) Remove the gitlink (no trailing slash):
5.) Cleanup the
.git/modules
:6.) Commit:
7.) Delete the now untracked submodule files