Git Tricks: Get the last checked out branch

10/14/2024

In this blog post I will show you two tricks how to easily switch between a branch back and forth and how to get the last checked out branches.

Switch between branches

Imagine you are on branch feature-1 and you want to switch to branch main to do some work there. After you are done with your work on main you want to switch back to feature-1. Without knowing the exact branch name you can easily use:

git checkout -

git switch - # since git 2.23

Getting the last 5 checked out branches

If you want to see the last 5 branches you checked out you can use the following command:

git reflog | grep -o 'checkout: moving from [^ ]* to [^ ]*' | awk '{print $NF}' | awk '!seen[$0]++' | head -n 5 

This works with the git bash and OS' like Linux and MacOS. The idea is to use the reflog to get the history of the branches you checked out. The grep command filters the output to only show the branches you checked out. The first awk command extracts the branch names from the output. The second awk command removes duplicates. The head command limits the output to the last 5 branches. So if you want only the last 3 branches you checked out you can change the head -n 5 to head -n 3.

git - useful aliases

In this article I'll show you some of my most used git aliases which make my everyday work easier and more productive.

.NET Tips and Tricks & ValueStringBuilder

I want to showcase two of my many side projects. My .NET Tips and Tricks website, where I collect and categorize, well, tips and tricks around .NET-related topics as well as my ValueStringBuilder.

How to rebase onto a different branch with git

We're all been there. You worked on a feature or bug on your branch and after some commits you noticed "Oops, I should have branched from xyz instead of abc". I'll show you an easy way of simply moving your stuff from branch abc to xyz.

An error has occurred. This application may no longer respond until reloaded. Reload x