Skip to content

Instantly share code, notes, and snippets.

@robballou
Last active December 30, 2015 18:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robballou/c0deb49f0e5a972fc6fe to your computer and use it in GitHub Desktop.
Save robballou/c0deb49f0e5a972fc6fe to your computer and use it in GitHub Desktop.
upstage() – update staging with changes from the current branch
# upstage()
#
# Update the staging branch with changes from the current branch. This will
# attempt to perform all the steps and return you to where you were. If there
# are problems along the way, you may get dumped out in between steps.
#
# Now accepts the branch and remote names if necessary:
#
# upstage [branch] [remote]
#
# Defaults to origin/staging.
#
# This amounts to:
#
# git checkout staging
# git fetch
# git reset --hard origin/staging
# git merge (the branch you were just on)
# git push origin staging
# git checkout (the branch you were just on)
#
function upstage() {
# text formatting blah-blah
bold=`tput bold`
normal=`tput sgr0`
if [[ -z "$1" ]]; then
staging='staging'
else
staging=$1
fi
if [[ -z "$2" ]]; then
remote='origin'
else
remote=$2
fi
# figure out the current branch
branch=$(git rev-parse --abbrev-ref HEAD)
if [[ $branch != "" ]]; then
# make sure we're not on staging already
if [[ $branch == $staging ]]; then
echo "🔨 $bold You are already on $staging $normal " >&2
return 0
fi
# check the status of the current branch, so we can tell if we can even
# switch to another branch.
branch_status=$(git status --porcelain)
if [[ $branch_status == "" ]]; then
echo "🔨 $bold Switching to the $staging branch $normal " >&2
git checkout $staging
if [[ $? -ne 0 ]]; then
return 1
fi
echo "🔨 $bold Updating $staging to match $remote $normal " >&2
git fetch $remote
git reset --hard $remote/$staging
if [[ $? -ne 0 ]]; then
return 1
fi
echo "🔨 $bold Merging in branch $branch $normal " >&2
git merge $branch
if [[ $? -eq 0 ]]; then
echo "🔨 $bold Merged! Updating $remote/$staging $normal " >&2
git push $remote $staging
if [[ $? -ne 0 ]]; then
return 1
fi
echo "🔨 $bold Returning to branch $branch $normal " >&2
git checkout $branch
return 0
else
echo "🔨 $bold Looks like there is some cleanup to handle $normal " >&2
fi
else
echo "🔨 $bold Is everything checked in? $normal " >&2
git status -sb
fi
fi
return 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment