Skip to content

Instantly share code, notes, and snippets.

@jeremytregunna
Created June 20, 2016 16:00
Show Gist options
  • Select an option

  • Save jeremytregunna/00625ceefd32324ae0dc6f3bc97a8707 to your computer and use it in GitHub Desktop.

Select an option

Save jeremytregunna/00625ceefd32324ae0dc6f3bc97a8707 to your computer and use it in GitHub Desktop.
#!/bin/sh
. git-simple-common.sh
if [ X"$1" = X"-h" ]; then
echo "Usage: git switch [branch]"
echo " Performs an optional stash of current changes before switching to [branch] where it"
echo " checks for any stashes that can be popped, and applies those."
exit 0
fi
# Exit early if we're already in this branch.
CURRENT_BRANCH_NAME=`current_branch_name`
if [ "${CURRENT_BRANCH_NAME}" = "$1" ]; then
die "Already in branch '$1'"
fi
# Check if we're in a dirty branch, and if so, stash.
stash_it "switching branches"
if [[ -n $1 ]]; then
TO_BRANCH=`git branch | sed -e 's/[ \*]*//g' | grep $1`
# Doesn't exist, uh oh.
[ X"$?" = X"1" ] && die "Branch '$1' does not exist. Did you maybe mean: git develop?"
# Switch the branch.
git checkout $1
if [ X"$?" = X"0" ]; then
# Switched the branch, here we need to check for a stash, apply it if there's one before exiting.
STASH_INDEX=`git stash list | grep ": On $1" | awk -F'[{}]' '$1 ~/stash@$/{print $2}'`
CURRENT_BRANCH=`git branch | grep "^\*" | sed -e 's/[ \*]*//g'`
if [[ -n ${STASH_INDEX} ]]; then
warn "Popping stash for branch ${current_branch_name}"
git stash pop "stash@{${STASH_INDEX}}"
fi
else
die "Unsure what happened. Aborting."
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment