Git branch command behaves like 'less'
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Since Git 2.16, git branch pipes its output through a pager (typically less) when the output exceeds the terminal height. This means the branch list opens in an interactive scrollable view instead of printing directly to the terminal. To disable this behavior, set git config --global pager.branch false or set the GIT_PAGER environment variable. This change catches many developers off guard, especially when scripting.
Why This Happens
Git 2.16 (released January 2018) changed git branch to use the pager by default. Previously, branch output was printed directly to stdout. The change was made for consistency with other Git commands like git log and git diff, which have always used a pager.
The pager is triggered when:
- The output is going to a terminal (not piped or redirected)
- The output exceeds the terminal height
- A pager is configured (default:
less)
Fix 1: Disable Pager for git branch Only
This is the most targeted fix — other commands like git log and git diff still use the pager.
Fix 2: Use --no-pager Flag
Fix 3: Change the Pager Program
The -F flag is particularly useful — it makes less behave like cat when the output fits on one screen, but still pages for long output.
Fix 4: Disable Pager Globally
This affects git log, git diff, git branch, and all other commands that use the pager.
Fix 5: Configure less Globally
With LESS='-FRX', the pager exits automatically for short output and preserves the output on screen after quitting.
Navigating less (When You Want the Pager)
When the pager is useful (long branch lists), these keys help:
Git Config Pager Precedence
Git determines the pager in this order (highest priority first):
GIT_PAGERenvironment variablecore.pagergit configPAGERenvironment variable- Default:
less
Per-command settings (pager.branch, pager.log, etc.) override core.pager for that specific command.
Scripting with Git Branch
In scripts, the pager can cause hangs because less waits for keyboard input:
Common Pitfalls
- Scripts hanging on git branch: When
git branchis called in a script without redirection, it openslessand waits for input, causing the script to hang. Always usegit --no-pager branchor pipe the output (git branch | cat) in scripts. - Setting core.pager to empty string disables colors: Setting
git config core.pager ''disables the pager but also loses color output because Git only emits ANSI colors when outputting to a pager. Usecore.pager catinstead to preserve colors. - Forgetting --global scope:
git config pager.branch falsewithout--globalonly applies to the current repository. Other clones still use the pager. Use--globalfor user-wide settings or--systemfor machine-wide. - LESS environment variable conflicting with Git: If
LESSis set to options that disable raw mode (e.g., without-R), Git's colored output shows raw escape codes instead of colors. Always include-Rin yourLESSvariable. - Expecting git branch output to be stable for parsing: The output format of
git branchvaries between Git versions and includes decorations (like* mainfor the current branch). For scripts, usegit for-each-reforgit branch --format='%(refname:short)'for stable, parseable output.
Summary
- Git 2.16+ pipes
git branchoutput through a pager (less) by default - Disable with
git config --global pager.branch falsefor branch only - Use
git --no-pager branchfor one-time pager bypass - Set
LESS='-FRX'solessexits automatically for short output and preserves screen content - In scripts, always use
git --no-pageror pipe output to avoid hanging on interactive pager input - Use
git for-each-refinstead ofgit branchin scripts for stable, parseable output
Related reading
- Git cancel a revert
- git cannot apply binary patch without full index line
- Git, cannot checkout branch - error, pathspec ''...'' did not match any file(s) known to git
- Git cannot checkout branch - error pathspec '...' did not match any files known to git
- Git Cannot see new remote branch
- Git can't undo local changes error path ... is unmerged
- Git checkout updating paths is incompatible with switching branches
- Git Checkout warning unable to unlink files, permission denied
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.