Skip to content

Instantly share code, notes, and snippets.

@hospadar
Last active October 16, 2024 19:38
Show Gist options
  • Select an option

  • Save hospadar/51e4b9123f6b6da55fcab950bd03339b to your computer and use it in GitHub Desktop.

Select an option

Save hospadar/51e4b9123f6b6da55fcab950bd03339b to your computer and use it in GitHub Desktop.
function rb
# Get the 10 most recently used branches with their last commit time
set branches (git for-each-ref --sort=-committerdate refs/heads/ --format='%(refname:short) -- %(committerdate:relative)' | head -n 10)
# Display the branches with an index
for index in (seq (count $branches))
echo (set_color -o red)"$index"(set_color normal)": $branches[$index]"
end
# Prompt the user to choose a branch
set -l choice
set -l prompt (set_color blue)"branch?"(set_color normal)' '
read -P "$prompt" choice
# Validate the input
if test "$choice" -ge 0 -a "$choice" -lt (count $branches)
# Extract the branch name based on user selection
set branch_name (echo $branches[(math $choice)] | awk '{print $1}')
# Checkout the selected branch
git switch $argv $branch_name
else
echo "Invalid selection. Please enter a number between 0 and 9."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment