3

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

torek
  • 448,244
  • 59
  • 642
  • 775
dsf
  • 1,203
  • 1
  • 6
  • 11
  • 1
    Technically that's not an alphabetic sort either, but the short answer is that Git doesn't have this built in at all. You *can* enumerate all references, or all refs within any part of the namespace, using `git for-each-ref`. Run them through a program that sorts them however you like and you will have the result you want. Write a script that runs `git for-each-ref refs/remotes/origin | sort -n`, perhaps. (You'll need a bit more than this, I'm just showing the skeleton.) – torek May 27 '21 at 07:41
  • Updating my previous comment: I was entirely wrong as Git has "version sort" built in after all, as part of `for-each-ref` and hence also part of `git branch` and `git tag`. This was all part of the cleanup in Git 2.7.0. It's not well documented. – torek May 28 '21 at 05:33
  • @torek 2.7 indeed: https://stackoverflow.com/a/32988664/6309, https://stackoverflow.com/a/33163401/6309 – VonC May 28 '21 at 06:51

2 Answers2

6

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
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • @VonC Hey although I accepted Joachim's answer. It would be great to see your method as an answer as well. It would be helpful even if it maybe complex. – dsf May 27 '21 at 09:35
  • @dsf It was based on https://superuser.com/a/1007255/141 – VonC May 27 '21 at 09:41
5

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
Hasturkun
  • 35,395
  • 6
  • 71
  • 104