Skip to content

Instantly share code, notes, and snippets.

@flatpickles
Last active January 3, 2016 09:39
Show Gist options
  • Save flatpickles/8444470 to your computer and use it in GitHub Desktop.
Save flatpickles/8444470 to your computer and use it in GitHub Desktop.
Add this to your .bashrc for easier git branch management. Use `b` to view your most recently modified 15 branches, in order of recency, with short hash, name, and commit message. Use `bco` for the same, but to additionally enter a number and checkout the corresponding branch.
branch_count=15
alias b="git for-each-ref --sort=-committerdate refs/heads/ --count=$branch_count --format='%(objectname:short)\\%(refname:short)\\%(contents:subject)' | column -ts'\\'"
function bco {
# display recent branches
b | cat -n;
# read which the user wants to checkout
echo -n "checkout branch number: ";
read line;
# validate input
re='^[0-9]+$'
if ! [[ $line =~ $re ]] ; then
echo "error: Not a number";
return;
fi
if (($line < 1)) || (($line > $branch_count-1)) ; then
echo "error: Out of bounds";
return;
fi
# figure out which this is and check it out
branches=($(git for-each-ref --sort=-committerdate refs/heads/ --count=$branch_count --format='%(refname:short)'));
branch=${branches[line-1]};
git checkout $branch;
}
@flatpickles
Copy link
Author

Validation assumes >= 15 local branches. I'll fix this eventually.

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