Skip to content

Instantly share code, notes, and snippets.

@m-nakamura145
Created September 11, 2017 04:22
Show Gist options
  • Save m-nakamura145/09c6c9d4bc980affd2bc31bdf0cf4935 to your computer and use it in GitHub Desktop.
Save m-nakamura145/09c6c9d4bc980affd2bc31bdf0cf4935 to your computer and use it in GitHub Desktop.
peco-git-branch-checkout
function peco-git-branch-checkout --description 'Check out a branch interactively'
set dir (basename $PWD)
git branch -a | peco --prompt="[$dir] Checkout branch:" --query="$argv" | tr -d ' ' > /tmp/branchname
set selected_branch_name (cat /tmp/branchname)
# Remove remote/ if its part of the branchname
switch $selected_branch_name
case '*-\>*'
set selected_branch_name (echo $selected_branch_name | perl -ne 's/^.*->(.*?)\/(.*)$/\2/;print')
case 'remotes*'
set selected_branch_name (echo $selected_branch_name | perl -ne 's/^.*?remotes\/(.*?)\/(.*)$/\2/;print')
end
set selected_branch_name (echo $selected_branch_name | sed -e 's/\*//g')
# Do the actual checkout
echo "Checking out $selected_branch_name"
git checkout $selected_branch_name
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment