How view branches sorted by date in specific format and order - Git tricks(2)

Let’s know how we can list all branches sorted by date.

View branch sorted by date

display a list of all local branches and sort them based on the date of their last commit.

# sort desc (-committerdate)
git branch --sort=-committerdate

# sort asc (committerdate)
git branch --sort=-committerdate

If you’d like to make a custom format you can use for-each-ref with --sort option and --format option to write your format.

git for-each-ref --sort='-committerdate' --format='%(refname)%09%(committerdate)' refs/heads

In the above command, I print the refname first then commitdate in — format option the output will be like this

refs/heads/branch_name commitdate
refs/heads/branch_name2 commitdate

# real output
refs/heads/new_design Wed Apr 20 12:24:21 2022 +0200

refs/heads/design_assets Wed Apr 20 11:30:30 2022 +0200

If you want to print the date first then refname, the option format will be like this --format='%(committerdate)%09%(refname)%'

Let’s say you don’t want to print refs/heads and want print just the branch name each time, how we can do this? by using sed.

git for-each-ref --sort='-committerdate' --format='%(refname)%09%(committerdate)' refs/heads | sed 's-refs/heads/--'

#output
branch_name commitdate
branch_name2 commitdate

#real output
new_design Wed Apr 20 12:24:21 2022 +0200

design_assets Wed Apr 20 11:30:30 2022 +0200

I find these commands incredibly helpful when returning to work from a weekend or just jumping from project to project. Hopefully, you can use these commands too!

Note

I already created a big article have 11 tricks in git and this trick one of them, if you’d like to see it, check this link

If you found this post useful please share it with your friends and follow me😍

This will help me a lot 🙏

Let’s connect on LinkedIn, Twitter

--

--

--

Software engineer, Problem solver, Geek.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Make Your First Simple Web Components (Part 1)

Installing and configuring PatternLab

5 Ways to Lazy Load React Components

TypeScript Generic Types

JavaScript

The Simple JavaScript. HTML 100.

How to send custom data to Intercom via API

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abdlrahmansaber

Abdlrahmansaber

Software engineer, Problem solver, Geek.

More from Medium

Take me back, please! - Git tricks (1)

Git : Understanding basic commands and some useful tips

This chart explains the data flow using git commands

My GitHub Diary.

5 Commands Line Every Programmer Should Know