如何在同一CLI终端中在屏幕上显示`git branch`的输出? [英] How to display output of `git branch` on-screen in same CLI terminal?

查看:139
本文介绍了如何在同一CLI终端中在屏幕上显示`git branch`的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉,我在表达问题时遇到了麻烦。我在CLI( zsh ,如果有关系)上运行,使用git版本 2.20.0 和macOS(请考虑我也在家里的Ubuntu系统上也遇到过它。)

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

问题:如何显示 git分支在同一个CLI屏幕上?即如果我运行,

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

$ git branch
$

它会弹出一个新的屏幕/窗口(在我的CLI内),向我显示所有分支。按下 q 关闭屏幕,并使我返回外壳。问题是我不记得我所拥有的分支的名称!我希望它们在屏幕上打印!

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

理想的输出,

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

但是它会在当前目录中创建一个新的文本文件,并且会变得混乱。记忆告诉我,这曾经是 git 的工作方式。最近发生了一些变化,这让我发疯了!有什么办法可以恢复以前的行为?

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?

推荐答案

这实际上是您的寻呼机的问题(可能是<$ c $

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

大多数终端仿真器都提供备用屏幕的概念。打开编辑器将切换到该备用屏幕;此屏幕中显示的文本保留在该屏幕中,并且 only 仅保留在此屏幕中。退出编辑器会切换回主屏幕,并且备用屏幕上的文字会消失,因此您可以返回命令行会话,而不会出现编辑器显示混乱的情况。如果这正是您想要的,并且在使用编辑器时使有些有意义,那么这很好。

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.

不幸的是,此处的实现是进行此切换对于使用光标寻址模式的一切,而 less 使用光标寻址模式。因此,这意味着通过 less 的管道输出也将切换到备用屏幕。

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.

有许多解决方法和修复程序。 less 本身最简单的方法是使用 -X 选项,如此错误报告 less 文档:

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或--no-init

禁止将termcap初始化和反初始化
字符串发送到终端。如果
反初始化字符串做了不必要的操作,例如
清除屏幕,有时这是理想的。

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

Git默认运行 less -FRX ,因此如果您(a)使用的是 less ,而(b)却没有得到 -FRX ,通过 core.pager (或<$ c $)检查是否覆盖了默认设置c> $ GIT_PAGER )和/或通过环境变量 LESS

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.

有些用户(包括我自己)真的非常讨厌这种交替屏幕切换,并希望我们的编辑器输出保留在屏幕上。在这里,一个更强大的技巧是完全禁用备用屏幕。但是,这比较困难。参见例如如何在OSX的Terminal.app中关闭备用屏幕?有些人真的非常喜欢并希望在关闭时将其打开:例如,参见屏幕:如何打开备用屏幕?(有关如何关闭它的更多链接)。

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

(我使用反编译terminfo,编辑掉alt屏幕转义序列,然后自己编译自己的terminfo方法。)

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

这篇关于如何在同一CLI终端中在屏幕上显示`git branch`的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆