-
-
Save likema/d470f48299195ed10955a7da0254c3dd to your computer and use it in GitHub Desktop.
Checkout back to the last used git branch like cd -
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Checkout develop branch
Checkout the specified branch
Checkout back to the last used branch