Last Updated: October 12, 2018
·
1.415K
· jacaetevha

Quickly navigating branches in Git

As a follow-up to my last tip, you can also add this alias to be able to select one of those branches:

[alias]
    switch-to-recent = "!switchtorecent() { PS3='select branch: '; select b in `git recent-branches $*` ; do echo \"switching to $b\"; git co $b; exit; done; }; switchtorecent"

Update: 2013-04-26, Added $* to pass arguments to recent-branches alias.

This will choose from the last 10 branches (the default in my previous post):

git switch-to-recent

If you want to change the number of branches to choose from (say to 20 instead of 10):

git switch-to-recent --count=20

2 Responses
Add your response

You can switch to last used branch by typing git checkout -

over 1 year ago ·

@sheerun Thanks. I'm aware of that. This alias allows you to jump back and forth between the last 10 branches you've been active on. git checkout -, as you pointed out, only allows you to go back to the branch you were on previously.

Honestly 10 is a high number of branches to be doing context switching. This alias could be tweaked to take a different count and pass that to the for-each-ref alias. However, 10 seemed like a nice round number and fits on the screen nicely.

over 1 year ago ·