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.
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 committerdatecan be used. - The post also points to the
git-for-each-refman page, highlighting numerous other fields available for sorting, such asauthordate,creatordate,contents:size(message size),push,refname(the default), andupstream.
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.