How to version-sort git branch output (vs the usual alphabetical/lexicographic sorting)

Issue

I use git branch -a to display branches.

I am assuming the git branch -a is not sorting alphabetically.

Need git branch -a to sort in numeric like 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 instead of 10, 11, 1, 2, 3, 4, 5, 6, 7, 8, 9

Consider, I don’t have the ability to rename branches like prefixing 0 as Part-02 for example as a workaround maybe.

I am open to third party command line git clients as well as a last resort.

On doing git branch -a

Current output:

  remotes/origin/Part-10_LoadStateListener
  remotes/origin/Part-11_Navigating-to-the-Detail-Screen
  remotes/origin/Part-12_Implementing-the-DetailsFragment
  remotes/origin/Part-13_Handling-Process-Death
  remotes/origin/Part-1_Project-Setup
  remotes/origin/Part-2_Layouts-&-Model-Class
  remotes/origin/Part-3_Navigation-Component
  remotes/origin/Part-4_API-Interface
  remotes/origin/Part-5_Dependency-Injection-with-Hilt
  remotes/origin/Part-6_PagingSource-&-PagingData
  remotes/origin/Part-7_PagingDataAdapter
  remotes/origin/Part-8_Header-&-Footer
  remotes/origin/Part-9_Search-Functionality

Expected output:

  remotes/origin/Part-1_Project-Setup
  remotes/origin/Part-2_Layouts-&-Model-Class
  remotes/origin/Part-3_Navigation-Component
  remotes/origin/Part-4_API-Interface
  remotes/origin/Part-5_Dependency-Injection-with-Hilt
  remotes/origin/Part-6_PagingSource-&-PagingData
  remotes/origin/Part-7_PagingDataAdapter
  remotes/origin/Part-8_Header-&-Footer
  remotes/origin/Part-9_Search-Functionality
  remotes/origin/Part-10_LoadStateListener
  remotes/origin/Part-11_Navigating-to-the-Detail-Screen
  remotes/origin/Part-12_Implementing-the-DetailsFragment
  remotes/origin/Part-13_Handling-Process-Death

I am hoping there is a flag like --numeric-sort so I could use git branch -a --numeric-sort

Solution

git itself doesn’t support rich sorting options for commands like this (edit: turns out that’s no longer true, see answer by Hasturkun for details), but thanks to the power of shell you can easily use external programs to sort the output for you.

And both GNU sort and BSD sort provide the -V option which works similarly to natural sort order, so something like

git branch -a | sort -V

should give you output like this:

  remotes/origin/Part-1_Project-Setup
  remotes/origin/Part-2_Layouts-&-Model-Class
  remotes/origin/Part-3_Navigation-Component
  remotes/origin/Part-4_API-Interface
  remotes/origin/Part-5_Dependency-Injection-with-Hilt
  remotes/origin/Part-6_PagingSource-&-PagingData
  remotes/origin/Part-7_PagingDataAdapter
  remotes/origin/Part-8_Header-&-Footer
  remotes/origin/Part-9_Search-Functionality
  remotes/origin/Part-10_LoadStateListener
  remotes/origin/Part-11_Navigating-to-the-Detail-Screen
  remotes/origin/Part-12_Implementing-the-DetailsFragment
  remotes/origin/Part-13_Handling-Process-Death

Answered By – Joachim Sauer

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published