#!/bin/bash # rebase-multiple.sh -- # * rebase local master to remote master, and # * rebase local git branches to updated local master # # Usage: # rebase-multiple.sh # rebase-multiple.sh --all # exit without a branch list or --all if [[ ( $# -eq 0 ) || ( $1 == "--all" && $# -gt 1 ) ]]; then echo -e "Usage:" \ "\n `basename $0` " \ "\n `basename $0` --all" >&2 exit 1 fi # remember where we started starting_branch=`git branch | sed -n 's/^\* //p'` return_from_branch () { if [[ $1 != $starting_branch ]]; then echo git checkout $starting_branch fi } rebase_abort_verbose () { echo "Aborting rebase on branch '$1'..." >&2 git rebase --abort git status } # exit now if we can't checkout master if [[ $starting_branch != master ]]; then git checkout master || exit 1 fi # update master if ! git pull --rebase; then rebase_abort_verbose master return_from_branch master echo -e "\nERROR: There was a problem with 'master'." \ "\n No branches were rebased." >&2 exit 1 fi # parse arguments if [[ $# -eq 1 && $1 == "--all" ]]; then # sed removes current branch `* master' from rebase_list # `--reverse' leaves local branches in order in gitk. rebase_list=`git branch | sed '/^\*/d' | sort --reverse` else rebase_list="$@" fi # rebase local branches to master where possible successful_branches="" failed_branches="" for branch in $rebase_list; do echo if git rebase master $branch; then successful_branches="$successful_branches\n$branch" else failed_branches="$failed_branches\n$branch" rebase_abort_verbose $branch fi done # return to starting branch return_from_branch $branch # report success/failure if [[ $successful_branches ]]; then successful_branches=`echo -e \ "$successful_branches\nmaster" | sort` echo -e "\nThe following branches are up to date:" for branch in $successful_branches; do git branch --no-color | sed -n "/^[* ] $branch$/p" done fi if [[ $failed_branches ]]; then failed_branches=`echo -e "$failed_branches" | sort` echo -e "\nThe following branches failed to rebase:" for branch in $failed_branches; do git branch --no-color | sed -n "/^[* ] $branch$/p" done exit 1 fi exit