Skip to content

Instantly share code, notes, and snippets.

@wilensky
Last active October 15, 2019 06:10
Show Gist options
  • Save wilensky/aafe1c09ae0edb2897bd74f051553f4a to your computer and use it in GitHub Desktop.
Save wilensky/aafe1c09ae0edb2897bd74f051553f4a to your computer and use it in GitHub Desktop.
Script checkouts each submodule on a given branch if latter exists (or fallback branch is used) and pulls if behind
#!/usr/bin/env bash
fetch=$(git fetch);
if [ "$(git show-ref refs/remotes/origin/$1)" ]; then # Checking branch for existence on remote
co=$(git checkout "$1"); # Checkouting branch that we checked for existence above
else
defaultFb="master"; # Default fallback
fb=${2:-$defaultFb} # Resulting fallback
echo "- Branch $1 does not exist. Switching to ${fb}";
co=$(git checkout $fb); # Checkouting fallback branch
fi
echo "$co"; # Showing output for consistency
if [[ $co == *"can be fast-forwarded"* ]]; then # Checking if we are behind the checkouted branch
echo "$(git pull)"; # Performing `pull`
fi
exit 0;
@wilensky
Copy link
Author

wilensky commented Feb 22, 2017

Installation

Wherever you are execute

cd ~ && \
curl -O https://gist.githubusercontent.com/wilensky/aafe1c09ae0edb2897bd74f051553f4a/raw/5de7216d8444b7faa255c4fe4764f7c5c14f9240/git_sm_co.sh && \
chmod a+x git_sm_co.sh && \
cd -

Usage

cd ~/some/project-with-submodules
git submodule foreach '~/git_sm_co.sh my-branch' # Default fallback branch is `master`
git submodule foreach '~/git_sm_co.sh my-branch some/fallback-branch' # Fallback branch is defined as `some/fallback-branch` and will be used if `my-branch` is not on repository

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