Ditch the shell hack: Git can sort branches by commit date natively
For years, developers drowning in stale git branches leaned on homegrown shell functions—looping over every branch, extracting commit dates with git log, and piping the mess through sort—just to see which branches were touched most recently. Modern git makes all of that unnecessary. A single setting, git config branch.sort committerdate, tells git branch to order its output by the last commit on each branch. The same behavior is available ad hoc via git branch —sort committerdate when you don’t want to change your global config.
The sort keys aren’t limited to commit dates. Because git branch shares its sorting machinery with git for-each-ref, any field that command understands is fair game. Useful options include authordate, creatordate, and taggerdate for alternative time-based ordering, refname for alphabetical sorting (the default), and upstream or push to organize branches by their tracking relationships. There’s even contents:size to sort by the byte length of a commit or tag message.
The broader lesson is a familiar one for anyone maintaining long-lived scripts: tooling evolves, and workflows people scripted around a decade ago are often now first-class features. Periodically checking whether a well-worn custom command has been obsoleted by the tool itself can retire a lot of accumulated cruft.
Read the full article
Continue reading at Hacker News →This is an AI-generated summary. Read the original for the full story.