Skip to content

Instantly share code, notes, and snippets.

@reececomo
Created March 9, 2025 22:26
Show Gist options
  • Select an option

  • Save reececomo/46b1ff2896c57ed60dbcfbdb8c5c3e3f to your computer and use it in GitHub Desktop.

Select an option

Save reececomo/46b1ff2896c57ed60dbcfbdb8c5c3e3f to your computer and use it in GitHub Desktop.
checkout_new_branch() {
local adjectives=(
ancientest boldest bravest brightest calmest cleverest coldest coolest craziest darkest
dustiest eagrest fanciest fastest fiercest flattest freshest funniest gentlest giantest
grandest greenest grimmest happiest harshest heaviest hottest iciest jolliest kindest largest
laziest lightest littlest loneliest loudest luckiest magicalest mightiest modernest narrowest
newest noblest oldest orangest palest perfectest pinkest plainest poorest proudest quickest
quietest rapidest rarest richest roundest roughest saddest sharpest shiniest shortest shyest
silentest simplest sleekest slowest smallest smartest smoothest softest solidest sourest
squarest steepest strongest sweetest swiftest tallest thickest thinnest tiniest toughest
truthfulest vastest warmest weakest whitest wildest wisest woodenest yellowest youngest
zaniest
)
local nouns=(
apple arrow badger bear bird breeze brook canyon cloud comet
crystal dawn deer dragon dusk eagle earth ember feather fire
flame flower fog forest fox frost gem ghost glacier hawk hill
horizon island jewel king lake leaf lion lizard meadow mist
moon mountain night ocean owl path peak pebble phoenix planet
rabbit rainbow river rock rose shadow shark sky snow spark
spirit star stone storm stream sun thunder tiger tree valley
wave whisper wind wolf wood zephyr
)
local adjective="${adjectives[RANDOM % ${#adjectives[@]}]}"
local noun="${nouns[RANDOM % ${#nouns[@]}]}"
local date=$(date +%Y%m%d)
local suffix=$(openssl rand -hex 4)
local branch_name="${adjective}-${noun}-${date}-${suffix}"
git checkout -b "$branch_name"
}
# Enhanced checkout with ordered tab completion for recent branches
# Usage:
# $ checkout <tab>
checkout() {
if [[ -z "$1" ]]; then
checkout_new_branch
else
git checkout "$@"
fi
}
co() {
checkout "$@"
}
_branch_suggestions() {
local current_branch=$(git branch --show-current)
local default_branch=$(git symbolic-ref refs/remotes/origin/HEAD | sed 's|^refs/remotes/origin/||')
local grey=$'\e[90m'
local success=$'\e[32m'
local reset=$'\e[0m'
local suggestion_count=5
local -a branches
branches=()
# default branch
if [[ "$current_branch" != "$default_branch" ]]; then
local default_time=$(git log -1 --format=%cr "$default_branch")
branches+=("$default_branch:$default_branch ${success}(default)${reset}")
fi
# suggest recent branches
branches+=(${(f)"$(git branch --sort=-committerdate --format='%(refname:short):%(refname:short) - '"$grey"'%(committerdate:relative)'"$reset" |
grep -v "^${current_branch}:" |
grep -v "^${default_branch}:" |
head -n "${suggestion_count}"
)"})
_describe -V 'branches' branches
}
compdef _branch_suggestions checkout
compdef _branch_suggestions co
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment