Git 分支命令的行为类似于“少”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48341920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:13:40  来源:igfitidea点击:

Git branch command behaves like 'less'

gitterminalzshgit-branchoh-my-zsh

提问by DenniJensen

When I use the git branchcommand to list all branches, I see the output of git branch | less.

当我使用git branch命令列出所有分支时,我看到git branch | less.

The command git branchis supposed to show a list of branches, like lsdoes for files.

该命令git branch应该显示分支列表,就像ls文件一样。

This is the output I get:

这是我得到的输出:

Enter image description here

在此处输入图片说明

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

我如何获得的默认行为git branch?什么导致分页输出?

I am using ZSH with oh_my_zsh(nothing for Git in there), and my .gitconfiglooks like this:

我正在使用 ZSH 和oh_my_zsh(没有用于 Git 的),我.gitconfig看起来像这样:

[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

回答by Zach Schneider

As mentioned in comments to Mark Adelsberger's answer, this was a default behavior change introduced in Git 2.16.

正如对 Mark Adelsberger 回答的评论中所述,这是Git 2.16 中引入的默认行为更改。

You can turn paged output for git branchback off by default with the pager.branchconfig setting:

git branch默认情况下,您可以使用pager.branch配置设置关闭分页输出:

git config --global pager.branch false

回答by Matthieu Moy

As other answers pointed out, Git defaults to piping itself into a pager (lessby default) for most commands.

正如其他答案所指出的那样,less对于大多数命令,Git 默认将自身管道化到寻呼机(默认情况下)。

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:

但是,重要的一点是,当 LESS 环境变量未设置时,Git 将其设置为 FRX,结果是用户可见的行为与当命令的输出很短时未使用寻呼机的行为相同(即如果你只有几个分支)。见人少

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

-F 或 --quit-if-one-screen
如果整个文件可以显示在第一个屏幕上,则使 less 自动退出。

-R 或 --RAW-CONTROL-CHARS
[...]ANSI“颜色”转义序列以“原始”形式输出。

-X 或 --no-init
禁止将 termcap 初始化和取消初始化字符串发送到终端。如果取消初始化字符串做了一些不必要的事情,比如清除屏幕,这有时是可取的。

If you get the behavior you describe, you most likely have $LESSset 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 $LESSas-is by adding this to your .gitconfigfile:

如果你得到你描述的行为,你很可能已经$LESS设置为其他的东西,unset LESS取消它 ( ) 将解决这个问题,同时保持“寻呼机”行为的长输出。或者,您可以$LESS通过将其添加到您的.gitconfig文件来激活行为,同时保持原样:

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

如果您真的不喜欢寻呼机,则可以全局或按命令停用它(请参阅其他答案)。

回答by Mark Adelsberger

Not to argue semantics, but the behavior you're getting isthe 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.

不要争论语义,但你得到的行为默认的。这就是为什么当你不要求不同的东西时你会得到它的原因。默认情况下,branch(以及许多其他 Git 命令)在将输出发送到终端时使用寻呼机。

You can override this default by using the --no-pageroption:

您可以使用以下--no-pager选项覆盖此默认值:

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-refin preference to git branch.)

或者,如果您将输出重定向到一个文件,Git 应该检测到它没有写入终端,因此无论如何都不应该使用寻呼机。(另一方面,这暗示了一个脚本用例,在这种情况下,您应该考虑使用像git for-each-ref优先于的管道命令git branch。)

回答by ionescu77

This Git behaviour was more and more annoying for me, too. I got my tag list in lesswhen just wanting to list tags for example.

这种 Git 行为对我来说也越来越烦人。less例如,当我只想列出标签时,我得到了我的标签列表。

One can control this behaviour also by changing the default Git PAGERto catinstead of less. I'd rather scroll in iTerm than in an editor. I like to use the editor when I want.

也可以通过将默认的Git PAGER更改为cat而不是来控制这种行为less。我宁愿在 iTerm 中滚动而不是在编辑器中滚动。我喜欢在需要时使用编辑器。

So:

所以:

git config --global core.pager cat

回答by Nick

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

对于那些想要更新~/.gitconfig以解决此问题的人,它看起来像这样:

[pager]
   branch = false

回答by Imran Ahmad

Git branch command behaves like 'less'

Git 分支命令的行为类似于“少”

Because Git by default opens the output in pager (at least in Ubuntu). The accepted answerwill completely replace pager, which you may not likeif your output is very long.

因为 Git 默认在 pager 中打开输出(至少在 Ubuntu 中)。该接受的答案将完全取代寻呼机,这你可能不喜欢,如果你的输出是很长。

I would recommend replacing the pager with less, so it doesn't "scroll" outputs less than the height of the terminal.

我建议用 替换寻呼机less,这样它就不会“滚动”输出小于终端的高度。

git config --global --replace-all core.pager "less -F -X"

回答by Jo?o Tiago

Do the following:

请执行下列操作:

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

回答by Yongwei Wu

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 branchnot use a pager always, which will not be desired when there is a lot of output.
  1. (默认配置)bash 和 zsh 之间的行为实际上是不同的。“问题”仅在 zsh 下出现。
  2. 建议的解决方案将git branch始终不使用寻呼机,这在有大量输出时是不需要的。

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 LESSin zsh, everything goes back to normal....

真正的原因是 bash 和 zsh 对 LESS 有不同的默认定义:bash 什么都不定义,而 zsh 将其定义为-R. 当我unset LESS在 zsh 中执行时,一切都恢复正常....

The -Rbehaviour may still be desired. In that case, you can add the following instruction to your .zshrc to keep everything working:

-R行为可能仍然是需要的。在这种情况下,您可以将以下指令添加到您的 .zshrc 以保持一切正常:

export LESS=-FRX

-F‘causes less to automatically exit if the entire file can be displayed on the first screen'. However, -Xneeds to be specified simultaneously, otherwise no output will be shown when there is less than a screenful of output.

-F'如果整个文件可以显示在第一个屏幕上,则导致 less 自动退出'。但是,-X需要同时指定,否则当输出少于一屏时将不显示任何输出。

回答by C-Otto

https://git-scm.com/book/en/v2/Git-Internals-Environment-Variables

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.

GIT_PAGER 控制用于在命令行上显示多页输出的程序。如果未设置,PAGER 将用作后备。

To solve your issue, you could unset PAGER and GIT_PAGER in your shell.

要解决您的问题,您可以在 shell 中取消设置 PAGER 和 GIT_PAGER。

回答by ad_on_is

I've had the same problem with git statusand git config --global pager.status falsesolves that too.

我也遇到过同样的问题git statusgit config --global pager.status false解决了。