July 23: Easy git checkout
Last updated: Oct 9, 2024
Contents
Hello again! This is going to be a short one. Ever had the moment when you are working in multiple branches and you have to switch between them? I have. Recently, I’ve been using two shortcut quite a lot.
- Git provides us a handy shortcut for this situation.
git checkout -
This command checkouts to the previous working branch. This is quite helpful when you’re juggling between 2 branches.
But for me, major problem arises when I have to juggle between multiple branches when few people are working on the same project, or when I have to work on multiple branches. This happens to me quite a lot for some reason.
- Git provides us ability to sort branch based on multiple parameters. I make use out of this to sort branches in descending order by commit date.
git branch --sort=-committerdate
This is good start. This shows me the branch I commited recently at the top. But it’s still a hassle to copy the branch name and checkout to it.
That’s where one of my favorite tools comes in. Behold the magic of fzf. In one line, it’s an interactive fuzzy finder for anything in terminal, and it’s really fast.
Using fzf, I’ve created an alias which lists all the branches sorted by commit date in interative terminal, where I can either type the name, or use arrow keys to select the required branch to checkout.
alias gbs="git branch --sort=-committerdate | fzf | xargs git checkout"
That’s it guys. Hope you have fun with the new power you’ve attained. Adios!