0

According to the doc:

If --list is given, or if there are no non-option arguments, existing branches are listed; the current branch will be highlighted with an asterisk.

Option -r causes the remote-tracking branches to be listed, and option -a shows both local and remote branches.

If a <pattern> is given, it is used as a shell wildcard to restrict the output to matching branches. If multiple patterns are given, a branch is shown if it matches any of the patterns. Note that when providing a <pattern>, you must use --list;
otherwise, the command is interpreted as branch creation.

But what is not specified is what is the order of the listing.
Does git branch -r --list "$PATTERN" output the branch name in some specified order?

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
Jim
  • 3,845
  • 3
  • 22
  • 47

2 Answers2

1

The output order is not affected by any pattern arguments.

The git branch command is a user-oriented interface layered atop git for-each-ref.1 The for-each-ref documentation describes the sorting in greater detail, but the git branch command's documentation includes this sub-description:

Sort based on the key given. Prefix - to sort in descending order of the value. You may use the --sort=<key> option multiple times, in which case the last key becomes the primary key. The keys supported are the same as those in git for-each-ref. Sort order defaults to sorting based on the full refname (including refs/... prefix). This lists detached HEAD (if present) first, then local branches and finally remote-tracking branches.

(emphasis mine). If your git branch porcelain front-end is too old to support --sort (pre-Git-2.7), you always get this default order.


1In some older versions of Git, there are things git branch can do that git for-each-ref cannot, so this is not strictly true in all versions of Git, it's just the way that the system has always been intended to work. Newer versions of git branch support the --sort option that git for-each-ref has supported for longer.

torek
  • 448,244
  • 59
  • 642
  • 775
  • Git 2.23 brings to `git branch --list` new sort options/behaviors: https://stackoverflow.com/a/56978829/6309 and https://stackoverflow.com/a/57046734/6309 – VonC Jul 15 '19 at 20:27
  • @VonC: the color for added work-trees' checked out branches is new; the fix to list detached HEAD first even in Chinese locales is, well, just a bug fix :-) – torek Jul 15 '19 at 20:55
1

The page you linked to also says (under the documentation for the --sort option):

Sort order defaults to sorting based on the full refname (including refs/... prefix). This lists detached HEAD (if present) first, then local branches and finally remote-tracking branches.

Richard Fearn
  • 25,073
  • 7
  • 56
  • 55