Jim
Jim

Reputation: 4415

Output of git branch list and how is it determined?

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?

Upvotes: 0

Views: 2068

Answers (2)

Richard Fearn
Richard Fearn

Reputation: 25491

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.

Upvotes: 1

torek
torek

Reputation: 488193

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.

Upvotes: 1

Related Questions