Skip to content

Instantly share code, notes, and snippets.

@erikpukinskis
Last active October 21, 2022 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikpukinskis/ceecc9d30713d17949be3723dd8ea6e0 to your computer and use it in GitHub Desktop.
Save erikpukinskis/ceecc9d30713d17949be3723dd8ea6e0 to your computer and use it in GitHub Desktop.
USAGE=$(cat <<-END
Usage:
b list all git branches, sorted by most recently edited
b 3 switch to branch 3
b clean 10+ delete all branches from 10 on
END
)
branches=( $(git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads) )
currentBranch=( $(git rev-parse --abbrev-ref HEAD) )
if [ -z "$1" ]
then
for ((i = 0; i <= ${#branches[@]} - 1; i++))
do
branch=${branches[i]}
if [ "$branch" == "$currentBranch" ]
then
echo -e " * $i\t$branch"
else
echo -e " $i\t$branch"
fi
done
exit
fi
if [ "$1" == "clean" ]
then
if ! [[ $2 =~ ^[0-9]+\+$ ]]
then
echo "$USAGE"
exit 1
fi
arg=$2
branchnum=${arg%?}
for (( i = $branchnum; i <= ${#branches[@]} - 1; i++ ))
do
git branch -D ${branches[$i]}
done
exit
fi
if ! [[ $1 =~ ^[0-9]+$ ]]
then
echo "$USAGE"
exit 1
fi
branchnum=$1
git checkout ${branches[$branchnum]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment