-
-
Save brunogama/ddba0d42b8b103d8dfe96592c323689b to your computer and use it in GitHub Desktop.
An alternative to "git submodule foreach --recursive git checkout develop"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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