Skip to content

Instantly share code, notes, and snippets.

@alexbbt
Created August 11, 2016 01:43
Show Gist options
  • Save alexbbt/ec6719b60a6f9f17c35aabe4239b145a to your computer and use it in GitHub Desktop.
Save alexbbt/ec6719b60a6f9f17c35aabe4239b145a to your computer and use it in GitHub Desktop.
Interactive Git Branch Selector
#!/bin/bash
TITLE="Branch Selector"
MENU="Select a branch to checkout:"
OPTIONS=()
tempBranches=()
BRANCHES=()
eval "$(git for-each-ref --shell --format='tempBranches+=(%(refname))' refs/heads/)"
i=0
max_width=0
for branch in "${tempBranches[@]}"; do
OPTIONS+=($i)
i="$((i+1))"
branch=${branch:11}
BRANCHES+=($branch)
OPTIONS+=($branch)
if [ "${#branch}" -gt "$max_width" ]; then
max_width="${#branch}"
fi
done
WIDTH="$((max_width+9))"
CHOICE_HEIGHT="$((i*2))"
HEIGHT="$((CHOICE_HEIGHT))"
CHOICE=$(dialog --keep-tite\
--clear \
--title "$TITLE" \
--menu "$MENU" \
$HEIGHT $WIDTH $CHOICE_HEIGHT \
"${OPTIONS[@]}" \
2>&1 >/dev/tty)
if [[ "$CHOICE" == "" ]]; then
exit 1
fi
git checkout ${BRANCHES[CHOICE]}
@andrewjensen
Copy link

Thanks for posting this! I stumbled across it while googling.

For Mac users, make sure to install the dialog dependency first. I used brew install dialog.

@hakunin
Copy link

hakunin commented Jan 18, 2017

This one is without dependencies: https://gist.github.com/shadowhand/3940027

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