Git 分支命令的行为类似于 'less'

Git branch command behaves like 'less'

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

命令 git branch 应该显示分支列表,就像 ls 对文件所做的那样。

这是我得到的输出:

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

我将 ZSH 与 oh_my_zsh 一起使用(其中没有 Git),我的 .gitconfig 看起来像这样:

[user]
  email = myemail@mail.com
  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

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.

要解决您的问题,您可以在 shell.

中取消设置 PAGER 和 GIT_PAGER

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

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

git --no-pager branch

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

, this was a default behavior change introduced in Git 2.16所述。

您可以使用 pager.branch config setting:

默认关闭 git branch 的分页输出
git config --global pager.branch false

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

不过,重要的一点是 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.

如果您得到描述的行为,您很可能将 $LESS 设置为其他内容,取消设置 (unset LESS) 将在保持 "pager" 长输出的行为。或者,您可以通过将此添加到 .gitconfig 文件来激活行为,同时保持 $LESS 不变:

[core]
    pager = less -FRX

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

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

[pager]
   branch = false

执行以下操作:

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

这种 Git 行为也让我越来越恼火。 例如,当我只想列出标签时,我在 less 中得到了我的标签列表。

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

所以:

git config --global core.pager cat

接受的答案似乎有误。有两个问题:

  1. (默认配置)bash 和 zsh 之间的行为实际上是不同的。 ‘问题’只出现在 zsh 下。
  2. 建议的解决方案将使 git branch 不总是使用分页器,当有大量输出时,这将是不希望的。

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

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

export LESS=-FRX

-F‘如果首屏可以显示整个文件,导致less自动退出’。但需要同时指定-X,否则输出不足一屏时不显示输出