Skip to content

How to list recently switched branches in git

Posted on:May 20, 2024 at 05:22 AM

To list recently switched branches in Git, you can use the following command

git reflog | grep "checkout" | head -n 10

Explanation

This command is a pipeline of three commands, each serving a specific purpose:

  1. git reflog
  2. grep "checkout"
  3. head -n 10

git reflog

This command displays the reflog of the current repository. The reflog is a history of all the changes to the tip of branches and other references, including branch switches, commits, resets, and more.

grep "checkout"

This command filters the output of git reflog to include only lines that contain the word “checkout”. In the context of Git, “checkout” entries in the reflog indicate when a branch switch occurred. This helps to narrow down the reflog to only the branch switch events.

head -n 10

This command takes the filtered output (lines containing “checkout”) and displays only the first 10 lines. The head command is useful for limiting the amount of output to make it easier to read and analyze.

recently I found another way to do the same:

git branch -v

this command that lists all branches with latest commits, you can use the --sort option too:

git branch -av --sort=committerdate // -a to see non local branches