git
git-branch
pattern
command-line
branch-list

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

bash
1# List all branches matching a pattern
2git branch --list 'feature/*'
3# feature/login
4# feature/payment
5# feature/user-profile
6
7# Match any branch with 'fix' in the name
8git branch --list '*fix*'
9# bugfix/login
10# hotfix-auth
11# quick-fix
12
13# Single character wildcard
14git branch --list 'release-?.0'
15# release-1.0
16# release-2.0
17# release-3.0

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

bash
1# All feature branches
2git branch --list 'feature/*'
3
4# All release branches
5git branch --list 'release/*'
6
7# Branches by developer prefix
8git branch --list 'john/*'
9
10# Branches with a ticket number
11git branch --list '*JIRA-123*'
12
13# Version branches (1.x, 2.x, etc.)
14git branch --list '[0-9]*'
15
16# Branches starting with specific letters
17git branch --list '[a-f]*'

Character Sets and Ranges

bash
1# Match branches starting with digits
2git branch --list '[0-9]*'
3
4# Match branches starting with lowercase letters a through f
5git branch --list '[a-f]*'
6
7# Match specific characters
8git branch --list '[fb]*'
9# feature/login
10# bugfix/auth
11
12# Negate a character set
13git branch --list '[!0-9]*'
14# All branches NOT starting with a digit

Square brackets define character sets. [a-z] matches any lowercase letter, [0-9] matches any digit, and [!...] or [^...] negates the set.

Multiple Patterns

bash
1# List branches matching either pattern
2git branch --list 'feature/*' 'bugfix/*'
3# bugfix/login
4# feature/payment
5# feature/user-profile
6
7# Combine with formatting
8git branch --list 'release/*' 'hotfix/*' --format='%(refname:short)'
9# hotfix/auth
10# release/1.0
11# release/2.0

Multiple patterns are OR-ed together. A branch appears in the output if it matches any of the provided patterns.

Remote and All Branches

bash
1# List remote branches matching a pattern
2git branch -r --list 'origin/feature/*'
3# origin/feature/login
4# origin/feature/payment
5
6# List all branches (local + remote) matching a pattern
7git branch -a --list 'feature/*'
8# feature/login
9# remotes/origin/feature/login
10# remotes/origin/feature/payment
11
12# Remote branches only
13git branch -r --list '*/release/*'
14# origin/release/1.0
15# upstream/release/2.0

Use -r for remote-tracking branches and -a for all branches. The --list flag must come before the pattern.

Why --list Is Required

bash
1# WITHOUT --list: Git thinks 'feature/*' is a new branch name
2git branch 'feature/*'
3# error: 'feature/*' is not a valid branch name
4
5# WITH --list: Git filters existing branches
6git branch --list 'feature/*'
7# feature/login
8# feature/payment
9
10# Without a pattern, --list is implied
11git branch
12# Equivalent to: git branch --list

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

bash
1# Sorted by last commit date, filtered by pattern
2git branch --list 'feature/*' --sort=-committerdate
3# feature/payment    (most recent)
4# feature/login
5# feature/user-profile (oldest)
6
7# With custom format
8git branch --list 'feature/*' --format='%(refname:short) %(committerdate:relative)'
9# feature/payment 2 days ago
10# feature/login 1 week ago
11
12# Merged branches matching a pattern
13git branch --merged main --list 'feature/*'
14
15# Count matching branches
16git branch --list 'feature/*' | wc -l

Practical Examples

bash
1# Delete all merged feature branches
2git branch --merged main --list 'feature/*' | xargs git branch -d
3
4# Find stale branches (older than 3 months)
5git branch --list '*' --sort=committerdate \
6  --format='%(committerdate:short) %(refname:short)' | head -20
7
8# Check if a specific branch exists
9git branch --list 'feature/login' | grep -q . && echo "exists" || echo "not found"
10
11# List branches with verbose info, filtered
12git branch -v --list 'release/*'
13# release/1.0 a1b2c3d Release 1.0 final
14# release/2.0 d4e5f6a Release 2.0 RC1

Common Pitfalls

  • Forgetting --list with filters: git branch -a 'feature/*' may create a branch named literally feature/* or throw an error instead of filtering. Always use git 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 [...]. Use git branch --list 'feature-[0-9]*' instead.
  • Pattern does not match remote prefix: git branch -r --list 'feature/*' matches nothing because remote branches are prefixed with origin/. Use git branch -r --list 'origin/feature/*' or git branch -r --list '*/feature/*'.
  • Case sensitivity on case-insensitive filesystems: On macOS (case-insensitive by default), git branch --list 'Feature/*' may match feature/* 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 --list explicitly 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/*'

Course illustration
Course illustration

All Rights Reserved.