tekin.co.uk

List your Git branches by recent activity

Even if you’re diligent and regularly delete merged and stale branches you may still find it hard to pick out a particular branch from the alphabetically-sorted output of git branch. How about something more useful, like seeing them listed based on their freshness?

The git branch command accepts a --sort option which we can use to list our branches based on the last committer date:

  $ git branch --sort=-committerdate
  * main
    redact-abandoned
    log-downloads
    i18n-lint

We can also use the --format option to include the exact time and see just how fresh each branch is:

  $ git branch --sort=-committerdate --format="%(committerdate)%09%(refname:short)"
  Thu Nov 25 10:29:48 2021 +0000  main
  Fri Nov 19 16:08:49 2021 +0000  redact-abandoned
  Fri Nov 19 16:04:11 2021 +0000  log-downloads
  Thu Nov 18 21:53:37 2021 +0000  i18n-lint
  Sun Jul 18 12:49:18 2021 +0100  without-routing-key-overrides

Or for something more friendly and easy-to-parse we can ask for a relative date:

  $ git branch --sort=-committerdate --format="%(committerdate:relative)%09%(refname:short)"
  5 hours ago     main
  6 days ago      redact-abandoned
  6 days ago      log-downloads
  7 days ago      i18n-lint
  4 months ago    without-routing-key-overrides

That’s better! Let’s add this as a git recent alias to our Git config:

  $ git config --global alias.recent 'branch --sort=-committerdate --format="%(committerdate:relative)%09%(refname:short)"'

Hat tip to Tenderlove for this particularly tasty snippet.

Here are some other Git related articles you might find useful:

Want more juicy Git tips like this straight to your inbox?

You'll get an email whenever I have a fresh insight or tip to share. Zero spam, and you can unsubscribe whenever you like with a single click.

More articles on Git

Authored by Published by