#!/bin/sh # Check out the first of the given branches that exists, # looking at both local branches and the ones on `origin` # # Usage: git checkout-first-existing-branch [ ...] set -eu if [ $# -eq 0 ]; then echo "Usage: git checkout-first-existing-branch [ ...]" >&2 exit 2 fi for _branch in "$@"; do if git show-ref --verify --quiet "refs/heads/$_branch" \ || git show-ref --verify --quiet "refs/remotes/origin/$_branch"; then exec git checkout "$_branch" fi done echo "None of these branches exists: $*" >&2 exit 1