Skip to content

Instantly share code, notes, and snippets.

@yoeunes
Last active January 5, 2022 20:58
Show Gist options
  • Save yoeunes/adb26ca2f234e49b8fed39d01d6647bb to your computer and use it in GitHub Desktop.
Save yoeunes/adb26ca2f234e49b8fed39d01d6647bb to your computer and use it in GitHub Desktop.
An alternative to "git submodule foreach --recursive git checkout develop"
#!/bin/bash
# add this function to your .bashrc or .zshrc and then run "source ~/.zshrc"
# - subcheckout param1 param2
# by default subcheckout will change to develop in every submodule
# if you pass a first parameter it will checkout to that branch if exist on submodule
# if you pass a second parameter as default branch if the first branch not exists
function subcheckout() {
branch=${1:-develop}
default=${2}
if [[ ! -d .git ]]; then
echo "not a git repo"
return 1
fi;
if ! [[ `git submodule status` ]]; then
echo 'not submodule'
return 1
fi
submodules=($(git config --file .gitmodules --get-regexp path | awk '{ print $2 }'))
currentDirectory=$(pwd)
for submodule in "${submodules[@]}"
do
printf "\n\nEntering '$submodule'\n"
cd "$currentDirectory/$submodule"
if [[ $(git rev-parse --verify --quiet ${branch}) ]]; then
git checkout ${branch}
elif ! [[ -z $default ]] && [[ $(git rev-parse --verify --quiet ${default}) ]]; then
git checkout ${default}
fi
done
cd "$currentDirectory"
}
@yoeunes
Copy link
Author

yoeunes commented Sep 6, 2020

Yes @JessicaMulein it's clean, thank youuu

@JessicaMulein
Copy link

There's an issue or two if you're in a nested repo and not at the root, but seems to work so far ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment