Vincent Hardouin

Switch to previous branches with git

When we work on a project with git, we regularly switch branches for different reasons: working on a new feature while another is in review, taking into account review feedbacks, doing a review, etc.

It can be interesting to quickly switch to a branch that we have recently used. We can use the git switch - command which allows us to switch to the last used branch.

However, sometimes we want to go back to a branch that was used long ago. Thanks to fzf, we can use the following command, which allows us to choose the branch we want to go back:

git reflog | grep checkout | grep -o 'to .*$' | grep -o ' .*$' | awk '!x[$0]++' | fzf | xargs git switch

command demo to go back to recent branch

Retour à la page d'accueil