List of Git branches ordered by most recent commit

In this answer, we’ll learn how to get a list of Git branches ordered by the most recent commit. There are two commands by which we can for this purpose.

The git branch command

The git branch command is used to perform list, create, or delete operations on git branches.

Syntax

git branch [OPTIONS]

Parameters

There are many options for this command, but one of the options that can be used is --sort. This option is used to sort based on the given key.

The committerdate can be used as the sort key to list the branches based on the commit date.

git clone https://github.com/apache/cassandra
cd cassandra
git branch -v --sort=committerdate

Code example

Let's look at the code below:

Terminal 1
Terminal
Loading...

Note: Copy-paste commands line by line to the terminal.

The git for-each-ref command

The git for-each-ref command is used to get the information on each ref.

Note: A ref is an indirect way of referring to a commit. It’s like a user-friendly alias for a commit hash.

Syntax

git for-each-ref [OPTIONS]

Parameters

There are many options for this command. some options are given below:

  • --sort: This option is used to sort based on the given key.

  • committerdate: This can be used as the sort key to list the branches based on the commit date.

git clone https://github.com/apache/cassandra
cd cassandra
git for-each-ref --sort=-committerdate refs/heads/

Code example

Let's look at the code below:

Terminal 1
Terminal
Loading...

Note: Copy-paste commands line by line to the terminal.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved