Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 735
dsf Avatar asked Nov 06 '25 07:11

dsf


1 Answers

You can ask git to do a version sort on refnames for you (as of git 2.7.0):

git branch -a --sort=v:refname

  remotes/origin/foo_1_bar
  remotes/origin/foo_2_bar
  remotes/origin/foo_3_bar
  remotes/origin/foo_4_bar
  remotes/origin/foo_5_bar
  remotes/origin/foo_6_bar
  remotes/origin/foo_7_bar
  remotes/origin/foo_8_bar
  remotes/origin/foo_9_bar
  remotes/origin/foo_10_bar
  remotes/origin/foo_11_bar

vs:

git branch -a

  remotes/origin/foo_10_bar
  remotes/origin/foo_11_bar
  remotes/origin/foo_1_bar
  remotes/origin/foo_2_bar
  remotes/origin/foo_3_bar
  remotes/origin/foo_4_bar
  remotes/origin/foo_5_bar
  remotes/origin/foo_6_bar
  remotes/origin/foo_7_bar
  remotes/origin/foo_8_bar
  remotes/origin/foo_9_bar
like image 124
Hasturkun Avatar answered Nov 07 '25 22:11

Hasturkun