Git branch command behaves like 'less'
Solution 1
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
Solution 2
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).
Solution 3
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
.)
Solution 4
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.
Solution 5
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
DenicioCode
Updated on July 12, 2022Comments
-
DenicioCode almost 2 years
When I use the
git branch
command to list all branches, I see the output ofgit branch | less
.The command
git branch
is supposed to show a list of branches, likels
does for files.This is the output I get:
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 = [email protected] 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
-
DenicioCode over 6 yearsUnset
PAGER
(GIT_PAGER
was unset) issue is still there. Are there any location I have to check this env vars? -
DenicioCode over 6 years
git --no-pager branch
works. But how is this set? Only thePAGER
environment variable is set toless
. This is not the default behaviour. All of my colleges have an output as a list printed out to the terminal. -
Lasse V. Karlsen over 6 yearsWhich version of git are you using? And which one are your colleagues using?
-
DenicioCode over 6 years2.16.0! Is this a feature introduced to git in this version?
-
DenicioCode over 6 yearsYeah it is default now. github.com/git/git/blob/master/Documentation/RelNotes/… My college next to me using the same version and does not have this issue :) thx @mark
-
Stragulus about 6 yearsThat is an odd, odd decision to make default. I expect my unix-y cli tools to behave like dumb simple programs that you can chain if you want to, but I guess that discussion is for another site.
-
Matthieu Moy about 6 yearsSee my answer: it's not really the default. If you (or your distro) did not set
$LESS
, you get a different behavior because Git sets it for you. -
Rory O'Kane almost 6 years@Stragulus Note that the new default doesn’t stop you from chaining
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
. -
mitnk over 5 years@RoryO'Kane but with pager, I have a type an extra
ESC/q
after a simplegit branch
check. -
Rory O'Kane over 5 years@mitnk Not necessarily. If your pager is
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. -
Sam over 5 yearsIf you want to add it into your config file manually, it's
[pager] branch = false
(on two lines). -
Ahmed Hasn. about 5 yearsstrange thing here: this setting was working and since some days it's not anymore, did anything change or Is my config just not being applied?
-
Tura about 5 yearsYeah this should be a valid answer for me
-
ionescu77 about 5 yearsthanks for the heads-up. I admit after a couple of months usage, I am still using cat, but for example when doing
git log
it is annoying. I just miss the original behaviour, but did not have time to improve my daily workflow in this pespective. -
Peemster almost 5 years@Sam's tip works the same for paginated results for
git diff
,git stash
etc. for anybody having this issue with those commands. Just add any variation ofstash = false
under a header of[pager]
in your .gitconfig file. -
David Douglas almost 5 yearsI use the following command to list remote branches using a pattern
git branch --list "*" -r | tr -d " *"
-
sdlins almost 5 yearsYou win!... Perfect!
-
joseph almost 5 yearsIn response to @RoryO'Kane's comment, you may want to do
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. -
codepleb over 4 yearsThis is the most superior answer here, sad that it has so few upvotes, because I scrolled quite a bit to find this masterpiece.
-
johankj over 4 yearsSetting
git config --global pager.branch 'less --quit-if-one-screen -R'
with a version of less >= 530 works great! -
maaartinus over 4 years@DenniJensen You can set the pager on the command like
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
. -
Fractalf over 4 years
git config --global core.pager ''
Will turn of the pager on all git commands -
Yongwei Wu about 4 yearsAre you really sure? The behaviour shows up only under zsh, but not bash. I happen to have both shells installed.
-
Yongwei Wu about 4 yearsAre you really sure? The behaviour shows up only under zsh, but not bash. I happen to have both shells installed.
-
Andre Nickatina about 4 yearsI hate when default behavior is changed leaving you to figure out how to revert it, and specifically I hate this behavior of git branch :/
-
gsscoder about 4 yearsWorks also with tag subcommand:
git config --global pager.tag false
. -
indivisible almost 4 years@ConquerorsHaki This solution is working for me with git v2.17.1 Try
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). -
0x6d6c over 3 yearsThere is yet another solution that binds features from both words:
cat
andless
: it'sbat
A cat(1) clone with wings. A tiny summary: for short output (fits on one screen) it behaves likecat
, and likeless
for long outputs (to large output for one screen). To me, it's almost a perfrect solution. -
ionescu77 over 3 yearsThis would be ideal. Interesting, did not know about
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. -
GDR over 3 yearsI wish I could hit the Up Vote more than once. Thank You
-
Marc Durdin over 3 yearsSome of the other answers give a reason as to why this behaviour differs between zsh and bash, and a better solution too (e.g. stackoverflow.com/a/60498979/1836776)
-
zyy over 3 yearsI had
pager = less -FR
as the optionX
seems to be disabling the mouse scrolling. -
Software Engineer over 3 yearsThis isn't true -- the behaviour shows up under most Linux variants when the LESS environment variable is set. zsh isn't special in this regard (or any other).
-
Yongwei Wu over 3 years@SoftwareEngineer What is not true? I said exactly that what mattered was whether LESS was set: ‘bash defines nothing, while zsh defines it to
-R
. When I dounset 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. -
Software Engineer over 3 yearsIn 1, you said that the behaviour only shows up in zsh. As I said, that's not true. The env var is set to -R in my ubuntu-bash too, so not just zsh. In 2, you implied that outputting to sdtout is not desirable when there is lots of output; also wrong. In my automated environments, paging is never desirable, no matter the length of the results. Given that automation is a large portion of operations these days I'd say that the stdout behaviour is highly desirable in a lot of cases, and 'maybe' less so for manual diagnostics and legacy work. So, this is wrong. The original answer is right.
-
Yongwei Wu over 3 years@SoftwareEngineer You completely failed to notice that the original question was asked about the behaviour under macOS (other systems, of course, may have different default behaviour). Take a closer look at the paths. — Your comment about the automated environments does not make sense as well, because paging is only enabled automatically in a TTY, but not when the output is redirected.
-
fpdude over 3 yearsThanks a lot! Doing cmd+f in the VSCode terminal to search for branch names wasn't working for me but adding
export LESS=-FRX
in my .zshrc fixed it! -
Tk421 about 3 yearsOne liner
git config --global core.pager "less -FRX"
-
drkvogel about 3 years
git --no-pager branch
seems best to me, so it helps you to remember the--no-pager
option and can do both easily. When looking at remotes e.g. withgit branch -a
, the list of branches can easily fill the screen, so paging is preferable, but when looking at your local branches, there often won't be so many, and you may want to list them on the command line so that you can remind yourself what branches you have and then do e.g.git checkout <branch-name>
. -
worc about 3 yearscan confirm
-X
flag being required to get-F
to work properly in zsh/Big Sur -
TxAG98 almost 3 yearsNot all heroes wear capes. Thank you for this!
-
bmaggi almost 3 yearsThis answer works and It doesn't need to modify my environment
-
Fer over 2 yearsactually you can decide which things are paged and which not. I absolutely want my git log being paged, but not the tag list (or the branches...) See the accepted answer
-
Erik Brendel about 2 yearsThanks, this is perfect: Keeping it simple for small outputs, while still having the benefits of a pager where needed
-
Alexa289 almost 2 yearsafter adding this answer to
.zshrc
file, I also need to runsource .zshrc
in terminal to make it works. thank you very much -
Yongwei Wu almost 2 years@Alexa289 Sourcing the .zshrc file should be automatic on all new sessions. It does not affect terminal sessions that are already started, though.