Skip to content

Instantly share code, notes, and snippets.

@moyaldror
Last active August 3, 2021 04:51
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save moyaldror/63b4c2b601592aa3ae8a317adec00a1c to your computer and use it in GitHub Desktop.
Save moyaldror/63b4c2b601592aa3ae8a317adec00a1c to your computer and use it in GitHub Desktop.
Complete remove of git submoule
#!/bin/bash
if [ $# -ne 1 ]; then
echo "Usage: $0 <submodule full name>"
exit 1
fi
MODULE_NAME=$1
MODULE_NAME_FOR_SED=$(echo $MODULE_NAME | sed -e 's/\//\\\//g')
cat .gitmodules | sed -ne "/^\[submodule \"$MODULE_NAME_FOR_SED\"/,/^\[submodule/!p" > .gitmodules.tmp
mv .gitmodules.tmp .gitmodules
git add .gitmodules
cat .git/config | sed -ne "/^\[submodule \"$MODULE_NAME_FOR_SED\"/,/^\[submodule/!p" > .git/config.tmp
mv .git/config.tmp .git/config
git rm --cached $MODULE_NAME
rm -rf .git/modules/$MODULE_NAME
rm -rf $MODULE_NAME
@moyaldror
Copy link
Author

@zx1986
Copy link

zx1986 commented Mar 7, 2019

nice!

@zhangyu911013
Copy link

zhangyu911013 commented Jul 23, 2019

nice script~but I find that when the submodule is the last in the .gitmodules file,the config isn't deleted correctly.
And I happened to find out another script that git provided works as well.

git config -f .gitmodules --remove-section submodule.$MODULE_NAME
git add .gitmodules
git config -f .git/config --remove-section submodule.$MODULE_NAME

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