Last active
January 3, 2016 09:39
-
-
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.
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
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Validation assumes >= 15 local branches. I'll fix this eventually.