Skip to content

Instantly share code, notes, and snippets.

@ianblenke
Created February 16, 2016 14:58
Show Gist options
  • Save ianblenke/21bc7ba721c7f91e6654 to your computer and use it in GitHub Desktop.
Save ianblenke/21bc7ba721c7f91e6654 to your computer and use it in GitHub Desktop.
Fix submodule remotes to match .gitmodules
#!/bin/bash
find . -name '.gitmodules' | while read gitmodules; do
parent_path="$(echo $gitmodules | sed -e 's/.gitmodules//')"
git config -f $gitmodules --get-regexp '^submodule\..*\.path$' |
while read path_key path
do
url_key=$(echo $path_key | sed 's/\.path/.url/')
gitmodules_url=$(git config -f $gitmodules --get "$url_key")
submodule_path=$(echo "$url_key" | sed -e 's/^submodule.//' -e 's/.url$//')
(
cd ./$parent_path/$submodule_path
remote_url=$(git remote -v | grep origin | head -1 | awk '{print $2}')
if [ "$gitmodules_url" != "$remote_url" ]; then
git remote rename origin old-$(date +%Y%m%d%H%M%S)
git remote add origin $gitmodules_url
git fetch --all
fi
)
done
done
git submodule update --init --recursive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment