Skip to content

Instantly share code, notes, and snippets.

@robballou
Created March 28, 2017 15:50
Show Gist options
  • Save robballou/f865a4dfd6fe797a2af9c0a6e647e04a to your computer and use it in GitHub Desktop.
Save robballou/f865a4dfd6fe797a2af9c0a6e647e04a to your computer and use it in GitHub Desktop.
Branch sub folders
#!/bin/bash
#
# Change git branches in all subfolders (that are git repos)
#
# Usage: ./resources/scripts/branch.sh BRANCH
#
set -e
if [[ "$#" -ne 1 ]]; then
2>&1 echo "Usage: $0 BRANCH"
exit 1
fi
FOLDERS=($(find . -type d -maxdepth 1))
for FOLDER in "${FOLDERS[@]}"; do
if [[ -d "$FOLDER/.git" ]]; then
pushd $FOLDER > /dev/null
current_branch=$(git rev-parse --abbrev-ref HEAD)
if [[ "$current_branch" != "$1" ]]; then
2>&1 echo "Changing branch for:" $FOLDER
git fetch
branch_exists=$(git branch | grep $1 | wc -l)
if [[ "$branch_exists" -eq 0 ]]; then
2>&1 echo "Created new branch:" $1
git checkout -b $1
else
git checkout $1
fi
fi
popd > /dev/null
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment