Last active
January 5, 2022 20:58
-
-
Save yoeunes/adb26ca2f234e49b8fed39d01d6647bb 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" | |
} |
JessicaMulein
commented
Sep 6, 2020
•
Yes @JessicaMulein it's clean, thank youuu
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