Skip to content

Instantly share code, notes, and snippets.

@mstudio
Created March 6, 2024 15:35
Show Gist options
  • Save mstudio/b7a4fea4f30ae51e5dfbc0b8ce35cd2b to your computer and use it in GitHub Desktop.
Save mstudio/b7a4fea4f30ae51e5dfbc0b8ce35cd2b to your computer and use it in GitHub Desktop.
Recursively update all git submodules in a direcory
#!/bin/bash
# Updates Git submodules to latest commit
# use:
# $bash update-submodules.sh WORKING_DIRECTORY SUBMODULE_DIRECTORY SHOULD_UPDATE_GIT
# sample use:
# $ bash update-submodules.sh ~/code/my-repo my-submodule true
update_submodule () {
# cd into parent directory of our submodule directory
cd "$1/../"
echo "***** Updating repository at $PWD *****"
# make sure that the parent directory actually has a submodule:
if test -f ".gitmodules"; then
# update the submodule
echo "***** Udating submodule $2 *****"
git submodule update --recursive --remote
# only update VCS if 3rd param exists
if [ "$3" ]; then
# add, commit and push changes
echo "***** Adding, committing, pushing *****"
git add $2
git commit -m "chore: updates submodule to latest commit"
git push
fi
fi
}
# save original directory
prev_dir="$PWD"
find $1 -type d -name "$2" -maxdepth 3 | while read file; do update_submodule "$file" "$2" "$3"; done
# revert to original directory
cd $prev_dir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment