#!/bin/sh branch_name=$1 # See if it already exists git rev-parse --verify --quiet $branch_name >/dev/null if [[ $? -eq 0 ]] ; then # Just checkout what they asked git checkout "$branch_name" else # Make sure we have the latest origin/$branch_name to branch git fetch exists_remote=$(git branch -r | grep -x " origin/$branch_name" | wc -l) if [[ $exists_remote -eq 0 ]] ; then # Specifying stable to get the last released code git checkout -b "$branch_name" origin/stable # Go ahead and put the branch out on the server git push origin "$branch_name" # Setup the merge property so that pulls come from the right place (instead of stable) git config --replace-all "branch.$branch_name.merge" "refs/heads/$branch_name" else # Do the checkout git checkout -b "$branch_name" "origin/$branch_name" fi fi