Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 59 lines (51 sloc) 1.56 KB
#!/bin/bash -e
## Remove orphaned submodule directories
## read input, display help if necessary
if [[ "$@" == *--help* ]]; then
Remove orphaned submodule directories
This command removed orphaned submodule directories, i.e., if a
submodule was removed (or han its url changed) but the directory was
not removed because it was not empty. Before removing a directory,
the script checks for any changes that might be lost. Afterwards, a
submodule sync is executed.
git rm-orphaned-submodule-dirs
exit 0;
## from the git mailinglist:
function git
LC_MESSAGES=C command git "$@"
export git
## ensure we are in the toplevel directory
cdup=$(git rev-parse --show-toplevel) &&
cd "$cdup" || {
echo >&2 "Cannot chdir to $cdup, the toplevel of the working tree"
exit 1
## find submodule urls not in .gitmodules:
git config --get-regexp submodule.*url |
while read key url; do
#echo "$key"
dir=`expr "$key" : 'submodule\.\(.*\)\.url'`
#echo "$dir"
if [[ -d "$dir" && $(git config -f .gitmodules --get "$key") != "$url" ]]; then
## Check if the directory is clean
cd "$dir"
git check-clean || exit 1
git check-unpushed || exit 1
git check-non-tracking || exit 1
## find the real git-dir (might be git-file, for v.1.7.8 compatibility)
## git dir should be relative to current dir... but is it?
gitdir=$(cd `git rev-parse --git-dir`; pwd)
cd "$tdir"
rm -rf "$dir"
## remove git dir as well (might git-file, for v.1.7.8 compatibility)
rm -rf "$gitdir"
git submodule sync > /dev/null
Something went wrong with that request. Please try again.