DEV Community

Ibrahim
Ibrahim

Posted on

How to Sort Git Branches by Last Commit Date

When I am working on a Git project, sometimes I want to switch to another branch, but I don't remember the branch name because it was created a long time ago.

To resolve this, I usually search for the branch by listing all branches sorted by date. This can be done using the git branch command with the --sort option set to committerdate. For example:

git branch --sort=committerdate
Enter fullscreen mode Exit fullscreen mode

The output will show all branches, with the oldest committer date appearing first.

  feat/authentication
  fix/timezone
  fix/auth-middleware
  feat/user-role-permission
  fix/logging-error
  develop
* main
Enter fullscreen mode Exit fullscreen mode

To display the most recent committer date first, use a - prefix before committerdate. For example:

git branch --sort=-committerdate
Enter fullscreen mode Exit fullscreen mode

The output will show all branches, with the latest committer date appearing first.

* main
  develop
  fix/logging-error
  feat/user-role-permission
  fix/auth-middleware
  fix/timezone
  feat/authentication
Enter fullscreen mode Exit fullscreen mode

To make it clearer, we can format the output to show the branch name along with the last modified date by adding the --format option, as shown below:

git branch --sort=-committerdate --format='%(committerdate:iso8601) %(refname:short)'
Enter fullscreen mode Exit fullscreen mode

Here is an example output:

2025-06-18T09:00:02+07:00 main
2025-06-14T13:45:12+07:00 develop
2025-06-10T08:22:30+07:00 fix/logging-error
2025-06-05T17:50:44+07:00 feat/user-role-permission
2025-05-28T10:10:00+07:00 fix/auth-middleware
2025-05-10T15:35:19+07:00 fix/timezone
2025-04-25T11:02:05+07:00 feat/authentication
Enter fullscreen mode Exit fullscreen mode

Top comments (0)