Skip to content

Instantly share code, notes, and snippets.

@dair-targ
Created September 20, 2013 12:32
Show Gist options
  • Save dair-targ/6636773 to your computer and use it in GitHub Desktop.
Save dair-targ/6636773 to your computer and use it in GitHub Desktop.
autocd for git
function err_handle()
{
status=$?
lastcmd=$(history | tail -1 | sed 's/^ *[0-9]* *//')
# Perform autocd
if [ -d "$lastcmd" ]
then
# Comment the following line out if autocd is enabled
cd $lastcmd
return
fi
# If the command is not a directory, it may be a branch name
ref_exists=$(git show-ref "refs/heads/$lastcmd")
if [[ "$ref_exists" ]]
then
git checkout "$lastcmd"
return
fi
# If the command is neither directory nor branch - try execute it as git command
if [[ "$lastcmd" =~ ^(add|commit|checkout|pull|push) ]]
then
git $lastcmd
return
fi
}
trap err_handle ERR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment