Skip to content

Instantly share code, notes, and snippets.

@brunogama
Forked from yoeunes/subcheckout
Created January 5, 2022 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunogama/ddba0d42b8b103d8dfe96592c323689b to your computer and use it in GitHub Desktop.
Save brunogama/ddba0d42b8b103d8dfe96592c323689b 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"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment