Skip to content

Instantly share code, notes, and snippets.

@JonathanWolfe
Created March 20, 2018 15:10
Show Gist options
  • Save JonathanWolfe/eb4b9c8a0498dc6b2fd9824325f98da3 to your computer and use it in GitHub Desktop.
Save JonathanWolfe/eb4b9c8a0498dc6b2fd9824325f98da3 to your computer and use it in GitHub Desktop.
Update all branches of repo (includes filter)
#!/bin/bash
# Merges the ${BASE} branch into all other branches
#
# Process:
#
# - Save the name of the current branch
# - If the current branch is not ${BASE} then checkout ${BASE}.
# - Pull the latest changes for ${BASE} from its upstream branch.
# - Loop over each remote branch.
# - If the branch is not ${BASE}.
# - Checkout the branch.
# - Merge ${BASE} into the branch.
# - Push changes if no conflicts
# - If the current branch is not the saved branch name checkout the saved
# branch name
DIRECTORY=$(pwd)
BASE="master"
FILTER="feature"
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-d|--dir|--directory)
DIRECTORY="$2"
shift # past argument
shift # past value
;;
-b|--base)
BASE="$2"
shift # past argument
shift # past value
;;
-f|--filter)
FILTER="$2"
shift # past argument
shift # past value
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
cd "${DIRECTORY}" || exit 1
REMOTE_BRANCHES=$(git for-each-ref --format="%(refname:lstrip=3)" refs/remotes/origin/${FILTER})
# Returns the name of the current branch
current_branch() {
git symbolic-ref --short HEAD
}
STARTING_BRANCH=$(current_branch)
if [[ "${STARTING_BRANCH}" != "${BASE}" ]]; then
echo "Swapping to base branch"
git checkout "${BASE}" -q
fi
git pull -q
for BRANCH in ${REMOTE_BRANCHES}; do
if [[ "${BRANCH}" != "${BASE}" ]]; then
echo "Updating: ${BRANCH}"
git reset --hard -q
git checkout "${BRANCH}" -f -q
git pull -q
git merge "${BASE}" -q
if git diff --check -q; then
git push --progress -q
fi
fi
done
echo
if [[ "${STARTING_BRANCH}" != "$(current_branch)" ]]; then
echo "Returning to starting branch"
git checkout "${STARTING_BRANCH}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment