Skip to content

Instantly share code, notes, and snippets.

@jwliechty
Created October 8, 2015 16:36
Show Gist options
  • Save jwliechty/9004da9a9966439bd019 to your computer and use it in GitHub Desktop.
Save jwliechty/9004da9a9966439bd019 to your computer and use it in GitHub Desktop.
Remove Git submodule
#!/bin/bash -e
# Based on article http://davidwalsh.name/git-remove-submodule
MODULE_PATH="${1:?You must speicify the module path}"
determinePaths(){
[ -d "${MODULE_PATH}" ] || die "Module directory does not exist: ${MODULE_PATH}"
MODULE_PATH="$( cd "${MODULE_PATH}"; pwd )"
GIT_ROOT="$( cd "$( dirname "${MODULE_PATH}" )"; git rev-parse --show-toplevel )"
MODULE_PATH="${MODULE_PATH#"${GIT_ROOT}/"}"
}
changeDirToGitRoot(){
cd "${GIT_ROOT}"
}
editGitModulesFile(){
local search="$( escapeForSearch "submodule \"${MODULE_PATH}\"" )"
local file=".gitmodules"
sed -i '/\['"${search}"'\]/,/^\[/ {
/\['"${search}"'\]/ d
/^\s/ d
/^\[/ n
}' "${file}"
git add "${file}"
}
editGitConfig(){
local search="$( escapeForSearch "submodule \"${MODULE_PATH}\"" )"
sed -i '/\['"${search}"'\]/,/^\[/ {
/\['"${search}"'\]/ d
/^\s/ d
/^\[/ n
}' ".git/config"
}
removeFromGit(){
git rm --cached "${MODULE_PATH}"
rm -rf ".git/modules/${MODULE_PATH}"
rm -rf "${MODULE_PATH}"
}
die(){
local msg="$1"
echo "${msg}" >&2
exit 1
}
escapeForSearch(){
local str="$1"
echo "${str}" | sed -e 's/[\/&]/\\&/g'
}
determinePaths
changeDirToGitRoot
editGitModulesFile
editGitConfig
removeFromGit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment