771

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:

git branch paging output

How do I get the default behaviour of git branch? What causes the paged output?

My .gitconfig looks like this:

[user]
  email = [email protected]
  name = Dennis H.
[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
0

10 Answers 10

1329

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
15
  • 284
    That 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.
    – Stragulus
    Apr 6, 2018 at 17:56
  • 13
    @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 commands git branch > branches.txt or git branch | wc -l. Jun 14, 2018 at 15:28
  • 38
    @mitnk Not necessarily. If your pager is less, you can add --no-init --quit-if-one-screen to your LESS environment variable, which will cause less to just write to stdout if the text can be viewed without scrolling. See man less for more details. Oct 17, 2018 at 7:38
  • 16
    If you want to add it into your config file manually, it's [pager] branch = false (on two lines).
    – Sam
    Oct 23, 2018 at 10:56
  • 8
    In 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.
    – joseph
    Aug 7, 2019 at 17:29
112

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).

3
  • 1
    I had pager = less -FR as the option X seems to be disabling the mouse scrolling.
    – zyy
    Nov 23, 2020 at 18:36
  • 10
    One liner git config --global core.pager "less -FRX"
    – Tk421
    Mar 4, 2021 at 23:36
  • 2
    Thanks, this is perfect: Keeping it simple for small outputs, while still having the benefits of a pager where needed May 11, 2022 at 7:20
103

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.)

10
  • 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. Jan 19, 2018 at 13:41
  • Which version of git are you using? And which one are your colleagues using? Jan 19, 2018 at 13:45
  • 2.16.0! Is this a feature introduced to git in this version? Jan 19, 2018 at 13:51
  • Yeah 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 Jan 19, 2018 at 13:55
  • 5
    This is the most superior answer here, sad that it has so few upvotes, because I scrolled quite a bit to find this masterpiece.
    – codepleb
    Sep 5, 2019 at 6:15
101

The accepted answer seems wrong. There are two problems:

  1. The behaviour is actually different between (default configured) bash and zsh. The ‘problem’ shows up only under zsh.
  2. 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.

8
  • 3
    This 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). Jan 31, 2021 at 12:25
  • 2
    @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 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.
    – Yongwei Wu
    Feb 1, 2021 at 15:22
  • In 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. Feb 1, 2021 at 15:58
  • 1
    @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.
    – Yongwei Wu
    Feb 3, 2021 at 2:16
  • Thanks 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! Feb 12, 2021 at 19:03
70

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
5
  • 3
    thanks 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.
    – ionescu77
    May 7, 2019 at 9:52
  • 3
    There is yet another solution that binds features from both words: 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.
    – 0x6d6c
    Aug 30, 2020 at 12:29
  • This 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.
    – ionescu77
    Aug 31, 2020 at 8:42
  • 1
    actually 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
    – Fer
    Dec 10, 2021 at 13:16
  • This is the best answer for me coming from terminal to iterm. Aug 15 at 9:58
16

For those that want to update their ~/.gitconfig to fix this, it would look like this:

[pager]
   branch = false
2
  • strange 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? Mar 4, 2019 at 21:04
  • @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). Jun 16, 2020 at 19:00
4

Do the following:

[alias]
  br = !git --no-pager branch
2

If you want to just see your branches (unformatted) you can pipe git branch to cat with:

git branch | cat
0

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.

2
  • 1
    Unset PAGER (GIT_PAGER was unset) issue is still there. Are there any location I have to check this env vars? Jan 19, 2018 at 13:24
  • @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.
    – maaartinus
    Jan 7, 2020 at 12:26
0

This work for me (Powershell under Windows)

$ git branch | echo

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.