Skip to content

Instantly share code, notes, and snippets.

@hedzr
Last active August 30, 2019 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hedzr/eff2a3b67efa3d3c52a0fc0d292182d5 to your computer and use it in GitHub Desktop.
Save hedzr/eff2a3b67efa3d3c52a0fc0d292182d5 to your computer and use it in GitHub Desktop.
Bundle to remove a git submodule
#!/bin/bash
# AUTHOR: Hedzr Yeh
# LICENSE: MIT
# PREREQUISITES:
# 1. install ini-op tool:
# i. via `go install`
# `go get github.com/hedzr/ini-op`
# ii. download ini-op bianry release and put the unzipped executable file into a location which is $PATH searchable
# `wget https://github.com/hedzr/ini-op/releases/download/v0.2.9/ini-op.darwin.amd64.gz | gunzip -c > /usr/local/bin/ini-op`
usage () {
local appname=$(basename $0)
cat <<EOF
'$appname' can remove an submodule from current git repo.
It do all clear stuffs for you, such as:
- git rm --cached third-party/libA
- rm -rf third-party/libA
- rm -rf .git/modules/third-party/libA
- erase the 'third-party/libA' section from .gitmodules
- erase the 'third-party/libA' from .git/config
Note:
'$appname' need an external tool 'ini-op'. download it from:
https://github.com/hedzr/ini-op/releases
and gunzip it to /usr/local/bin, or /usr/bin, or anywhere in
your \$PATH.
Usage: $appname submodule-pathspec
Sample:
\$ git submodule add git@github.com:Tencent/libco.git third-party/libco
\$ $appname third-party/libco
EOF
}
submodule-rm () {
local smn=${1:-nothing}
if [ "$smn" == "nothing" ]; then
usage
exit
elif [ -d .git ]; then
git rm --cached $smn 2>/dev/null || echo "submodule $smn may not exist."
rm -rf $smn && echo "$smn folder removed."
[ -d .git/modules/$smn ] && rm -rf .git/modules/$smn
[ -f .gitmodules ] && ini-op section rm "submodule \"$smn\"" .gitmodules
[ -f .git/config ] && ini-op section rm "submodule \"$smn\"" .git/config
else
echo "BAD: no .git folder found."
fi
}
submodule-rm "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment