Skip to content

Instantly share code, notes, and snippets.

@chris-kobrzak
Last active July 13, 2017 11:07
Show Gist options
  • Save chris-kobrzak/eddb15578ce3f4f381cc832f22cd2ac3 to your computer and use it in GitHub Desktop.
Save chris-kobrzak/eddb15578ce3f4f381cc832f22cd2ac3 to your computer and use it in GitHub Desktop.
Capture all feature branches in Git, update base branch (default `master`), rebase forks on it and, optionally, force push to the central repository.
#/usr/bin/env bash
baseBranch=master
if [[ $# -eq 1 ]]; then
baseBranch=$1
fi
echo "Update the repository..."
git fetch -p
echo
echo "Switch to the $baseBranch branch"
git checkout $baseBranch
# This assumes no one's rewritten the base branch's history in the meantime
git merge
echo
featureBranches=`git branch --no-merged | egrep -v "(^\*|master|dev|gh\-pages)"`
echo "Branches to be rebased:"
echo "$featureBranches"
echo
while read -r branch; do
git checkout "$branch"
git rebase # merge remote branch even if its history's been rewritten
git rebase $baseBranch
if [[ $? -eq 1 ]]; then
echo
echo "Encountered a rebasing issue, exiting"
exit 1
fi
echo
done <<< "$featureBranches"
echo "Publishing your rebased feature branches"
echo
for branch in $featureBranches; do
echo Do you wish to force push branch "$branch"?
select answer in "Yes" "No" "Exit"; do
case $answer in
Yes )
git checkout "$branch"
git push --force-with-lease origin "$branch"
echo
break 1
;;
No )
echo
echo "In case you wanted to publish this rebased branch manually:"
echo git checkout "$branch"
echo git push --force-with-lease origin "$branch"
echo
break 1
;;
Exit )
exit 0
esac
done
done
git checkout $baseBranch
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment