7

My apologies, but I am having trouble phrasing my question. I'm running on CLI (zsh if it matters), using git version 2.20.0 and on macOS (think I've encountered it on my Ubuntu system at home too).

Question: How do you display the output of git branch on the same CLI screen? i.e. if I run,

$ git branch
$

it pops open a new screen/window (inside my CLI) showing me all my branches. Pressing q closes the screen and returns me to shell. The problem is that I do not remember the names of the branches I had! I want them printed on-screen!!

Ideal output,

$ git branch
* master
branch_01
branch_02
temp_branch
experiment_2_delete
$

A dubious work-around is to do the following,

$ git branch > stuff.txt
$ cat stuff.txt
* master
branch_01
branch_02 
...
$

But it creates a new text file in the current directory and that gets messy. Memory tells me that this used to be the way git worked. Something changed recently and it's driving me nuts! Is there any way to get back the old behaviour?

chronodekar
  • 2,616
  • 6
  • 31
  • 36
  • Basically, I want to ask how to print git branches on CLI, but that is not _exactly_ the issue and folks will get confused. Spent an *agonizing* 30-ish minutes typing this question in. – chronodekar Jan 23 '19 at 16:29
  • @phd reading that question (& answer) - yes, this is a duplicate. `git config --global pager.branch false` was exactly what I needed! – chronodekar Jan 23 '19 at 17:45
  • For the record; I did use the search feature, but couldn't find that question. It also didn't appear as an alternate question when I was creating this question. – chronodekar Jan 23 '19 at 17:46
  • That happens a lot with duplicates, especially when it's not clear what's going on, which is why duplicate questions aren't bad. I'd still recommend making sure your less setup defaults to using `-X` (and maybe `-F` too, and `-R` to make colors work). – torek Jan 23 '19 at 18:43
  • You can also add `[core] pager = less -FRX` in your ~/.gitconfig file. – Boyan Jan 25 '21 at 12:13

1 Answers1

5

This is actually an issue with your pager (probably less these days).

Most terminal emulators offer the concept of an alternate screen. Opening your editor switches to this alternate screen; text displayed in this screen remains in this screen, and only in this screen. Exiting the editor switches back to the main screen, and the alternate screen text vanishes, so that you're back to your command-line session, without the editor's display cluttering things up. Which is fine if that's what you wanted, and makes some sense when using the editor.

Unfortunately, the implementation here is to do this switching for everything that uses cursor-addressing modes, and less uses cursor-addressing modes. So this means that piping output through less also switches to the alternate screen.

There are numerous work-arounds and fixes. The simplest for less itself is to use the -X option, as described in this bug report and the less documentation:

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

Note that Git defaults to running less -FRX, so if you (a) are using less and (b) are not getting -FRX, check to see if you've overridden the defaults, through core.pager (or $GIT_PAGER) and/or through the environment variable LESS.

Some users (including myself) really, really hate this alternate-screen switching and wish for our editor output to remain on the screen. Here, a more powerful trick is to disable the alternate screen entirely. This is harder, however. See, e.g., How can you turn off alternate screen in OSX's Terminal.app? Some people really, really like this behavior and want to turn it on when it's off: see, e.g., screen: how to turn on alternate screen? (which has more links to how to turn it off).

(I use the "decompile the terminfo, edit out the alt-screen escape sequences, and compile my own terminfo" method myself.)

torek
  • 448,244
  • 59
  • 642
  • 775
  • 2
    this is an informative post, but https://stackoverflow.com/a/48370253/198660 is more of what I was looking for. Thanks @torek. – chronodekar Jan 23 '19 at 17:47