Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/sh
root=`git rev-parse --show-toplevel`
echo=/bin/echo
git ls-files --stage | grep '^160000 ' |
while read mode sha1 stage path
do
$echo -n "Submodule path '$path': ";
cd $root; cd $path
track_branch=`git config --file $root/.gitmodules submodule.$path.track`
if [ -z "$track_branch" ]; then
$echo 'tracking branch not found; skip.'
else
$echo -n "track $track_branch... "
current_branch=`git symbolic-ref --quiet HEAD | sed 's#^refs/heads/##'`
local_branch=${track_branch#*/}
if [ "$current_branch" = "$local_branch" ]; then
$echo "already on $current_branch."
git pull
else
if git rev-parse --quiet --verify $local_branch > /dev/null; then
$echo "checkout existing branch $local_branch."
git checkout $local_branch && git pull
else
$echo "start tracking $track_branch."
git fetch && git checkout --track $track_branch
fi
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.