47

As title, I have read the manual but in vain.

What I found is that a * can be wildcard pattern matching.

git branch --list 'issues*6'
 issues/586
 issues/616

However, it's found by myself instead of mentioned in manual page.

I wonder what is the real format of <pattern>.

1 Answer 1

51

Quoting from that same manual page you linked:

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.

So the answer, at least according to the documentation, is that "it is used as a shell wildcard". This assumes, of course, that you know what the phrase "shell wildcard" means—and more importantly, it's wrong, since a straight shell wildcard would not match across the /.

The documentation should say something like: "The pattern acts much like a shell wildcard / glob pattern, except that slashes are not treated specially, so that a*b matches both accb and ac/cb, and a[bc/]* matches all of a/d, abcd, ac/cb, and accb."

Examples:

$ git branch -a
  a/d
  abcd
  ac/cb
  accb
* master
$ git branch --list 'a*b'
  ac/cb
  accb
$ git branch --list 'a[bc/]*'
  a/d
  abcd
  ac/cb
  accb
$ 
4
  • 4
    Is there a way to pass a negation pattern?
    – mljrg
    May 12, 2021 at 23:18
  • 1
    @mljrg: no. For complex operations, use git for-each-ref and write a script.
    – torek
    May 13, 2021 at 9:12
  • 4
    @mljrg: torek et al's answer suggests a type of negation to try and in fact it works. You can negate on a per character position basis. For example to list all not "old/" branches you can specify git branch --list '[^o]*' 'o[^l]*' 'ol[^d]*' 'old[^/]*' 'o' 'ol' 'old'. That is extremely cumbersome to get exactly what you want, but if you need a pattern more than once you can set it as an alias. Tested in git 2.37.1. Sure wish they had supported .gitignore syntax with its ! option. Aug 11, 2022 at 20:17
  • @SensorSmith: Yes, that's why I just summarized it as "no". (I've done this sort of thing with glob patterns in shells before, it's just too painful to use in practice...)
    – torek
    Aug 11, 2022 at 22:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.