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/cassandracd cassandragit branch -v --sort=committerdate
Code example
Let's look at the code below:
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
refis 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/cassandracd cassandragit for-each-ref --sort=-committerdate refs/heads/
Code example
Let's look at the code below:
Note: Copy-paste commands line by line to the terminal.
Free Resources