What is the format of pattern in git-branch --list
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The git branch --list <pattern> command filters branches using shell glob patterns (fnmatch), not regular expressions. The pattern supports * (match any characters), ? (match one character), and [...] (match character sets). For example, git branch --list 'feature/*' lists all branches starting with feature/. Multiple patterns can be specified. The --list flag is required when combining patterns with other options like -a or -r. Without --list, Git may interpret the pattern as a new branch name.
Basic Pattern Syntax
The pattern uses fnmatch-style globbing — the same pattern matching used in shell file globbing. * matches zero or more characters (except nothing before a / in some contexts), ? matches exactly one character.
Common Patterns
Character Sets and Ranges
Square brackets define character sets. [a-z] matches any lowercase letter, [0-9] matches any digit, and [!...] or [^...] negates the set.
Multiple Patterns
Multiple patterns are OR-ed together. A branch appears in the output if it matches any of the provided patterns.
Remote and All Branches
Use -r for remote-tracking branches and -a for all branches. The --list flag must come before the pattern.
Why --list Is Required
When options like -a, -r, or --sort are combined with a pattern, --list is mandatory. Otherwise Git may interpret the pattern as a branch to create or an ambiguous argument.
Combining with Other Options
Practical Examples
Common Pitfalls
- Forgetting
--listwith filters:git branch -a 'feature/*'may create a branch named literallyfeature/*or throw an error instead of filtering. Always usegit branch -a --list 'feature/*'when combining patterns with flags. - Not quoting the pattern:
git branch --list feature/*without quotes lets the shell expand*against local files before Git sees it. Always quote patterns with single quotes:git branch --list 'feature/*'. - Expecting regex syntax:
git branch --list 'feature-[0-9]+'does not work because+is a regex quantifier, not a glob metacharacter. Globs only support*,?, and[...]. Usegit branch --list 'feature-[0-9]*'instead. - Pattern does not match remote prefix:
git branch -r --list 'feature/*'matches nothing because remote branches are prefixed withorigin/. Usegit branch -r --list 'origin/feature/*'orgit branch -r --list '*/feature/*'. - Case sensitivity on case-insensitive filesystems: On macOS (case-insensitive by default),
git branch --list 'Feature/*'may matchfeature/*branches. On Linux (case-sensitive), it will not. Write patterns that match the exact case of your branch naming convention.
Summary
git branch --list <pattern>uses shell glob syntax (fnmatch), not regex*matches any characters,?matches one character,[a-z]matches character ranges- Always quote patterns in single quotes to prevent shell expansion
- Use
--listexplicitly when combining patterns with-a,-r,--sort, or other flags - Multiple patterns are OR-ed:
git branch --list 'feature/*' 'bugfix/*' - Remote branches require the remote prefix:
git branch -r --list 'origin/feature/*'

