HN
Today

Sort branches by last commit date

Many developers wrestle with an unwieldy number of Git branches, often resorting to complex custom scripts to keep track of recent activity. This post reveals a remarkably simple, built-in Git configuration that sorts branches by their last commit date. It's a prime example of a "Today I Learned" moment, streamlining a common development pain point with a single, elegant command.

3
Score
0
Comments
#6
Highest Rank
10h
on Front Page
First Seen
Aug 30, 7:00 PM
Last Seen
Aug 31, 4:00 AM
Rank Over Time
1967697681113

The Lowdown

The author shares a common predicament among developers: managing a multitude of Git branches and the subsequent challenge of quickly identifying the most recently active ones. Historically, this often led to the creation of intricate shell scripts to sort branches, a solution the author himself maintained for years. However, a simpler, more efficient method exists within modern Git.

  • The author previously used a custom Bash function that iterated through Git branches, extracted their last commit date, and then piped the output to sort.
  • The revelation is that modern Git allows direct configuration to sort branches. A single command, git config branch.sort committerdate, permanently sets the default branch listing to be ordered by the committer date.
  • For a one-off sorting operation without changing the configuration, git branch --sort committerdate can be used.
  • The post also points to the git-for-each-ref man page, highlighting numerous other fields available for sorting, such as authordate, creatordate, contents:size (message size), push, refname (the default), and upstream.

This simple Git configuration eliminates the need for convoluted scripting, offering a native and more efficient way to manage and view Git branches based on their activity, a welcome productivity boost for any developer.