When I use the git branch
command to list all branches, I see the output of git branch | less
.
The command git branch
is supposed to show a list of branches, like ls
does for files.
This is the output I get:
https://i.stack.imgur.com/rh4td.png
How do I get the default behaviour of git branch
? What causes the paged output?
I am using ZSH with oh_my_zsh
(nothing for Git in there), and my .gitconfig
looks like this:
[user]
email = myemail@mail.com
name = Dennis Haegler
[push]
default = simple
[merge]
tool = vimdiff
[core]
editor = nvim
excludesfile = /Users/dennish/.gitignore_global
[color]
ui = true
[alias]
br = branch
ci = commit -v
cam = commit -am
co = checkout
df = diff
st = status
sa = stash
mt = mergetool
cp = cherry-pick
pl = pull --rebase
[difftool "sourcetree"]
cmd = opendiff \"$LOCAL\" \"$REMOTE\"
[mergetool "sourcetree"]
cmd = /Applications/SourceTree.app/Contents/Resources/opendiff-w.sh
\"$LOCAL\" \"$REMOTE\" -ancestor \"$BASE\" -merge \"$MERGED\"
trustExitCode = true
As mentioned in comments to Mark Adelsberger's answer, this was a default behavior change introduced in Git 2.16.
You can turn paged output for git branch
back off by default with the pager.branch
config setting:
git config --global pager.branch false
As other answers pointed out, Git defaults to piping itself into a pager (less
by default) for most commands.
An important point, though, is that when the LESS environment variable is unset, Git sets it to FRX, and the consequence is that the user-visible behavior is the same as if the pager was not used when the command's output is short (i.e. if you have only few branches). See man less:
-F or --quit-if-one-screen Causes less to automatically exit if the entire file can be displayed on the first screen. -R or --RAW-CONTROL-CHARS [...]ANSI "color" escape sequences are output in "raw" form. -X or --no-init Disables sending the termcap initialization and deinitialization strings to the terminal. This is sometimes desirable if the deinitialization string does something unnecessary, like clearing the screen.
If you get the behavior you describe, you most likely have $LESS
set to something else, and unsetting it (unset LESS
) would get rid of the issue while keeping the "pager" behavior for long output. Alternatively, you can activate the behavior for while keeping $LESS
as-is by adding this to your .gitconfig
file:
[core]
pager = less -FRX
If you really dislike the pager thing, you can deactivate it globally or on a per-command basis (see other answers).
pager = less -FR
as the option X
seems to be disabling the mouse scrolling.
git config --global core.pager "less -FRX"
Not to argue semantics, but the behavior you're getting is the default. That's why you get it when you don't ask for something different. By default, branch
(and numerous other Git commands) use a pager when sending output to the terminal.
You can override this default by using the --no-pager
option:
git --no-pager branch
Or if you redirect the output to a file, Git should detect that it isn't writing to a terminal and so should not use a pager anyway. (On the other hand, that suggests a scripting use case, in which case you should consider using a plumbing command like git for-each-ref
in preference to git branch
.)
git --no-pager branch
works. But how is this set? Only the PAGER
environment variable is set to less
. This is not the default behaviour. All of my colleges have an output as a list printed out to the terminal.
The accepted answer seems wrong. There are two problems:
The behaviour is actually different between (default configured) bash and zsh. The ‘problem’ shows up only under zsh. The suggested solution will make git branch not use a pager always, which will not be desired when there is a lot of output.
The real reason is that bash and zsh have different default definitions about LESS: bash defines nothing, while zsh defines it to -R
. When I do unset LESS
in zsh, everything goes back to normal....
The -R
behaviour may still be desired. In that case, you can add the following instruction to your .zshrc to keep everything working:
export LESS=-FRX
-F
‘causes less to automatically exit if the entire file can be displayed on the first screen’. However, -X
needs to be specified simultaneously, otherwise no output will be shown when there is less than a screenful of output.
-R
. When I do unset LESS
in zsh, everything goes back to normal....’ As to whether this happens, it is that defaultly-configured bash does not define LESS, while defaultly-configured oh-my-zsh defines LESS. I know this because I had exactly the same oh-my-zsh environment in macOS (just like the asker), and I encountered exactly the same problem.
export LESS=-FRX
in my .zshrc fixed it!
This Git behaviour was more and more annoying for me, too. I got my tag list in less
when just wanting to list tags for example.
One can control this behaviour also by changing the default Git PAGER to cat
instead of less
. I'd rather scroll in iTerm than in an editor. I like to use the editor when I want.
So:
git config --global core.pager cat
git log
it is annoying. I just miss the original behaviour, but did not have time to improve my daily workflow in this pespective.
cat
and less
: it's bat
A cat(1) clone with wings. A tiny summary: for short output (fits on one screen) it behaves like cat
, and like less
for long outputs (to large output for one screen). To me, it's almost a perfrect solution.
bat
, it's not on my mac bin, I see there's a link to the github bat project. I'll have a look. Thx.
For those that want to update their ~/.gitconfig
to fix this, it would look like this:
[pager]
branch = false
git config --list --show-origin
to debug what settings are/aren't set for you/your system and from where (stackoverflow.com/q/12254076/1590950).
Do the following:
[alias]
br = !git --no-pager branch
https://git-scm.com/book/en/v2/Git-Internals-Environment-Variables
GIT_PAGER controls the program used to display multi-page output on the command line. If this is unset, PAGER will be used as a fallback.
To solve your issue, you could unset PAGER and GIT_PAGER in your shell.
PAGER
(GIT_PAGER
was unset) issue is still there. Are there any location I have to check this env vars?
PAGER= git branch
(with one space after and none before the equals sign exactly like written). No idea, whether it's any better than e.g., git branch | cat
.
Success story sharing
git branch
with something else. Using pipe detection, Git will output the branch list to stdout within the commandsgit branch > branches.txt
orgit branch | wc -l
.less
, you can add--no-init --quit-if-one-screen
to yourLESS
environment variable, which will causeless
to just write to stdout if the text can be viewed without scrolling. Seeman less
for more details.[pager] branch = false
(on two lines).export LESS = "--no-init --quit-if-one-screen -R"
(with -R) in your .zshrc, .bash_profile, etc., to honor raw color control characters. Otherwise, your git log will suffer.