-
-
Save alexbbt/ec6719b60a6f9f17c35aabe4239b145a to your computer and use it in GitHub Desktop.
Interactive Git Branch Selector
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/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]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for posting this! I stumbled across it while googling.
For Mac users, make sure to install the
dialog
dependency first. I usedbrew install dialog
.