Last active
December 23, 2016 18:24
-
-
Save jsgarvin/e3b66eeb0cf3439f4c2d17e7d4384007 to your computer and use it in GitHub Desktop.
List recently checked out git branches and quickly jump to one
This file contains hidden or 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
# Add to your .bashrc or sourced alias file | |
# | |
# Without any arguments, lists the last 9 branches you've been | |
# on, prompts for a number, and then switches you back to the | |
# corresponding branch. | |
# | |
# When passed a number as an argument, instantly switches you | |
# to the corresponding branch. This is particularly useful for | |
# toggeling back to your previous branch with `grb 1`. | |
function grb() { | |
if [ $# -eq 0 ]; then | |
branches=$(git reflog | grep 'moving from' | head -10 | tail -9 | sed -e 's/.* to \(.*\)$/\1/'); | |
i=1 | |
for branch in $branches; do | |
echo "$i) $branch" | |
((i++)) | |
done | |
read -n 1 -p "# " choice | |
if [[ $choice =~ ^[1-9]$ ]]; then | |
echo | |
git checkout @{-$choice} | |
else | |
echo "Invalid selection: $choice" | |
fi | |
else | |
if [[ $1 =~ ^[1-9]$ ]]; then | |
git checkout @{-$1} | |
else | |
echo "Invalid selection: $1" | |
fi | |
fi | |
} |
This file contains hidden or 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
$ grb | |
1) some_feature_branch | |
2) the_ugly_buggy | |
3) some_other_branch | |
4) master | |
5) staging | |
6) some_feature_branch | |
7) staging | |
8) debug_that_thing | |
9) some_feature_branch | |
# 8 | |
Switched to branch 'debug_that_thing' | |
# Or, if you're feeling lucky... (best if just toggeling back to previous branch with `grb 1`) | |
$ grb 8 | |
Switched to branch 'debug_that_thing' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment