Skip to content

Instantly share code, notes, and snippets.

@pawlik
Created July 25, 2018 14:10
Show Gist options
  • Save pawlik/832c2e2eba15f6b62e30908626134e6e to your computer and use it in GitHub Desktop.
Save pawlik/832c2e2eba15f6b62e30908626134e6e to your computer and use it in GitHub Desktop.
This allows you to switch between latest git branches
alias gsb="select option in `git branch --sort=-committerdate | head -n $(( $LINES / 2 )) | grep -v '*'`; do git checkout $option; break ;done"
# --sort=-committerdate # sorts branches by commit dates (use --sort=-committerdate for opposite direction)
# $(( $LINES / 2 )) # use half of your terminal height (I like to have some context preserved).
# use -n 100000000 to use all terminal space (select will reduce the number of options for you)
# grep -v '*' - exclude the current branch, if you remove this you have to accomodate the '*' char in $option
# break - remove this if you want to switch, and the immediatelly be given option to switch again :D (just a joke, it's useless without it)
@pawlik
Copy link
Author

pawlik commented Jul 25, 2018

demo:

$ gsb
1) [redacted]                                                                           
2) [redacted]                                                                                          
3) [redacted]                                                                            
4) master                                                                                                            
5) [redacted]                                                              
6) [redacted]                                                                                                  
7) [redacted]                                                            
8) [redacted]                                                                               
9) junk                                                                                                              
10) [redacted]                                                                         
11) [redacted]                                                                              
12) [redacted]                                                                        
?# 9
Switched to branch 'junk'

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