Skip to content

Instantly share code, notes, and snippets.

@likema
Last active March 15, 2017 09:14
Show Gist options
  • Save likema/d470f48299195ed10955a7da0254c3dd to your computer and use it in GitHub Desktop.
Save likema/d470f48299195ed10955a7da0254c3dd to your computer and use it in GitHub Desktop.
Checkout back to the last used git branch like cd -
#!/bin/sh
current_branch=`git rev-parse --abbrev-ref HEAD`
if [ -z "$current_branch" ]; then
echo "You are not in a git repository." >&2
exit 1
fi
branch=${1:-develop}
[ "$current_branch" = "$branch" ] && exit 0
git_dir=`git rev-parse --show-toplevel`
if [ -z "$git_dir" ]; then
echo "You are not in a git repository." >&2
exit 1
fi
old_gcb_cache=$git_dir/.oldgwd
[ -f "$old_gcb_cache" ] && old_gcb=`cat $old_gcb_cache`
[ "$branch" = '-' ] && branch=$old_gcb
[ -z "$branch" ] && branch=develop
if ! git rev-parse --verify $branch 2> /dev/null >&2; then
echo "The branch $branch does not exist." >&2
exit 1
fi
git checkout $branch
[ "$old_gcb" = "$current_branch" ] || echo "$current_branch" > $old_gcb_cache
@likema
Copy link
Author

likema commented Mar 15, 2017

Checkout develop branch

gco

Checkout the specified branch

gco feature/test

Checkout back to the last used branch

gco -

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment