(../../favicon.ico) (../../css/site.css) (../../css/archives.css) (Ryan Greenberg: TIL) (https://ryangreenberg.com/til/rss.xml) Sort branches by last commit date (index.html) (../../_astro/Layout.9hxnkgUX.css) (https://ryangreenberg.com/) (Home) Ryan Greenberg Version 3.1 (https://ryangreenberg.com/archives/) Blog (https://ryangreenberg.com/til/) TIL (https://ryangreenberg.com/projects/) Projects (https://ryangreenberg.com/about/) About (https://ryangreenberg.com/rss.xml) (https://ryangreenberg.com/til/) Today I Learned (https://ryangreenberg.com/til/by-date/) By Date (https://ryangreenberg.com/til/by-category/) By Category (https://ryangreenberg.com/2026/05/today-i-learned/) About (index.html) TIL: Sort branches by last commit date August 24, 20269:55 AM I have too many git branches and like many people I developed a convoluted script to show me the most recent ones. I ran this command many times a day for years: function branches { for k in ` git branch | sed " s/^..// "` ; do echo -e ` git log --color -1 --pretty=format: " %Cgreen%ci %Cblue%cr%Creset " " $k "` \\t " $k " ; done | sort } This outputs the date as the first column in the output so that sort works. Turns out with modern git it’s totally unnecessary. Just configure git branch to sort by the last commit date: git config branch.sort committerdate You can also supply this on a one-off basis as git branch --sort committerdate . What else can you sort by? The manual says “The keys supported are the same as those in git-for-each-ref(1) .” The manpage for (https://man.freebsd.org/cgi/man.cgi?query=git-for-each-ref&sektion=1&format=html) git-for-each-ref lists a ton of fields, including some that don’t make sense to me as sort keys. Here are a few that stood out as interesting: authordate , committerdate , creatordate , taggerdate contents:size : The size in bytes of the commit or tag message. push : The name of a local ref which represents the @{push} location for the displayed ref. refname : The name of the ref (the part after $GIT_DIR/) (this is the default). upstream : The name of a local ref which can be considered “upstream” from the displayed ref. Git About I'm Ryan. I'm a software engineer with training in a variety of barely related fields. I studied philosophy at (http://www.nd.edu) (University of Notre Dame) Notre Dame and I have a degree from UC Berkeley's (http://ischool.berkeley.edu) (UC Berkeley School of Information) School of Information . I worked at (https://twitter.com) Twitter , (https://slack.com) Slack , and (https://tern.sh) Tern . Colophon This site was built with (https://astro.build/) Astro . Previous versions ran on (http://movabletype.com/) (MovableType CMS) MovableType and (https://www.blogger.com/) Blogger . Latest Posts (https://ryangreenberg.com/2026/08/3d-shoebox-divider/) Printing a Shoebox Divider (https://ryangreenberg.com/2026/07/twitter-hall-of-fame/) Twitter Hall of Fame (https://ryangreenberg.com/2026/07/summer-of-shrub/) Summer of Shrub 2.0 (https://ryangreenberg.com/2026/06/a-year-of-mondays/) A Year of Mondays (https://ryangreenberg.com/2026/06/3d-printing-in-five-acts/) 3D printing in five acts © 2001 – 2026 by Ryan Greenberg. (https://creativecommons.org/licenses/by-nc-nd/4.0/) Some rights reserved .