How to display output of `git branch` on-screen in same CLI terminal? [duplicate]

霸气de小男生 提交于 2019-11-28 07:06:39

问题


This question already has an answer here:

  • Git branch command behaves like 'less' 8 answers

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?


回答1:


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



来源:https://stackoverflow.com/questions/54331536/how-to-display-output-of-git-branch-on-screen-in-same-cli-terminal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!