如何防止“git diff”使用寻呼机?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2183900/
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-10 07:46:47  来源:igfitidea点击:

How do I prevent 'git diff' from using a pager?

git

提问by Lakshman Prasad

Is there a command line switch to pass to git diffand other commands that use the lesspager by default?

是否有命令行开关可以传递给默认情况下git diff使用less寻呼机的其他命令?

I know I can pipe it to cat, but that removes all the syntax highlighting.

我知道我可以通过管道将它传递给 cat,但这会删除所有语法突出显示。

I know I can set the pager in the global .gitconfig to cat by GITPAGER=cat(or something like that); but I want to have pager sometimes (depending on the size of the diff).

我知道我可以将全局 .gitconfig 中的寻呼机设置为 cat by GITPAGER=cat(或类似的东西);但有时我想使用寻呼机(取决于差异的大小)。

But, I would prefer a command line switch if there is one; and I am not able to find one, going through the man pages.

但是,如果有的话,我更喜欢命令行开关;我无法通过手册页找到一个。

回答by Ignacio Vazquez-Abrams

--no-pagerto Git will tell it to not use a pager. Passing the option -Fto lesswill tell it to not page if the output fits in a single screen.

--no-pagerGit 会告诉它不要使用寻呼机。如果输出适合单个屏幕-Fless则传递选项将告诉它不分页。

Usage:

用法:

git --no-pager diff

Other options from the comments include:

评论中的其他选项包括:

# Set an evaporating environment variable to use 'cat' for your pager
GIT_PAGER=cat git diff

# Tells 'less' not to paginate if less than a page
export LESS="-F -X $LESS"
# ...then Git as usual
git diff

回答by ishaaq

As a previous answer mentioned, passing the -Foption to lesscauses it to quit if the content is less than one screen. However, after doing so, the screen is reset, and you end up not seeing the content.

正如前面的答案所提到的,如果内容少于一个屏幕,传递-F选项会less导致它退出。但是,执行此操作后,屏幕将重置,您最终将看不到内容。

The -Xoption does away with that behaviour. So, I use the following to enable conditional paging based on the amount of content:

-X选项消除了这种行为。因此,我使用以下内容根据内容量启用条件分页:

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

回答by geekQ

Use

git config --global core.pager cat

to get rid of a pager for all commands for all repositories.

摆脱所有存储库的所有命令的寻呼机。

You can also disable paging for single Git subcommands by using pager.<cmd>setting instead of core.pager, and you can change your settings per Git repository (omit --global).

您还可以通过使用pager.<cmd>setting 而不是禁用单个 Git 子命令的分页core.pager,并且您可以更改每个 Git 存储库的设置(省略--global)。

See man git-configand search for pager.<cmd>for details.

查看man git-config和搜索pager.<cmd>详细信息。

回答by VonC

The recent changes in the documentation mention a different way of removing a default option for less("default options" being FRSX).

文档中最近的更改提到了删除默认选项的不同方式less(“默认选项”是FRSX)。

For this question, this would be (git 1.8+)

对于这个问题,这将是 (git 1.8+)

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

For example, Dirk Bestersuggests in the comments:

例如,德克·贝斯特( Dirk Bester)在评论中建议:

export LESS="$LESS -FRXK" 

so that I get colored diff with Ctrl-Cquit from less.

这样我就可以通过Ctrl-C退出来获得彩色差异less

Wilson Fmentions in the commentsand in his questionthat:

威尔逊˚F提到的意见,并在他的问题是:

less supports horizontal scrolling, so when lines are chopped off, less disables quit-if-one-screen so that the user can still scroll the text to the left to see what was cut off.

less 支持水平滚动,因此当行被截断时,less 会禁用退出如果一个屏幕,以便用户仍然可以向左滚动文本以查看被截断的内容。



Those modifications were already visible in git 1.8.x, as illustrated in "Always use the pager for git diff" (see the comments). But the documentation just got reworded (for git 1.8.5 or 1.9, Q4 2013).

这些修改已经在 git 1.8.x 中可见,如“始终使用寻呼机git diff”中所示(请参阅评论)。但是文档刚刚改写(对于 git 1.8.5 或 1.9,Q4 2013)。

Text viewer for use by Git commands (e.g., 'less').
The value is meant to be interpreted by the shell.

The order of preference is:

  • the $GIT_PAGERenvironment variable,
  • then core.pagerconfiguration,
  • then $PAGER,
  • and then the default chosen at compile time (usually 'less').

When the LESSenvironment variable is unset, Git sets it to FRSX
(if LESSenvironment variable is set, Git does not change it at all).

If you want to selectively override Git's default setting for LESS, you can set core.pagerto e.g. less -+S.
This will be passed to the shell by Git, which will translate the final command to LESS=FRSX less -+S. The environment tells the command to set the Soption to chop long lines but the command line resets it to the default to fold long lines.

供 Git 命令使用的文本查看器(例如,'less')。
该值旨在由外壳程序解释。

优先顺序是:

  • $GIT_PAGER环境变量,
  • 然后core.pager配置,
  • 那么$PAGER
  • 然后是在编译时选择的默认值(通常是“少”)。

LESS环境变量未设置时,Git 将其设置为FRSX
(如果LESS设置了环境变量,Git 根本不会更改它)。

如果你想有选择地覆盖 Git 的默认设置LESS,你可以设置core.pager为 egless -+S
这将由 Git 传递给 shell,它会将最终命令转换为LESS=FRSX less -+S. 环境告诉命令设置S选项以截断长行,但命令行将其重置为默认值以折叠长行。



See commit 97d01f2afor the reason behind the new documentation wording:

有关新文档措辞背后的原因,请参阅提交 97d01f2a

config: rewrite core.pagerdocumentation

配置:重写core.pager文档

The text mentions core.pagerand GIT_PAGERwithout giving the overall picture of precedence. Borrow a better description from the git var(1)documentation.

The use of the mechanism to allow system-wide, global and per-repository configuration files is not limited to this particular variable. Remove it to clarify the paragraph.

Rewrite the part that explains how the environment variable LESSis set to Git's default value, and how to selectively customize it.

文中提到core.pager,并GIT_PAGER没有给予优先的大局。从git var(1)文档中借用更好的描述。

允许系统范围、全局和每个存储库配置文件的机制的使用不限于此特定变量。删除它以澄清段落。

重写解释如何将环境变量LESS设置为 Git 的默认值以及如何选择性地自定义它的部分。



Note: commit b327583(Matthieu Moy moy, April 2014, for git 2.0.x/2.1, Q3 2014) will remove the S by default:

注意:commit b327583Matthieu Moymoy,2014 年 4 月,用于 git 2.0.x/ 2.1,2014年第三季度)将默认删除 S:

pager: remove 'S' from $LESS by default

寻呼机:默认情况下从 $LESS 中删除 'S'

By default, Git used to set $LESSto -FRSXif $LESSwas not set by the user.
The FRXflags actually make sense for Git (Fand Xbecause sometimes the output Git pipes to less is short, and Rbecause Git pipes colored output).
The Sflag (chop long lines), on the other hand, is not related to Git and is a matter of user preference. Git should not decide for the user to change LESS's default.

More specifically, the Sflag harms users who review untrusted code within a pager, since a patch looking like:

-old code;
+new good code; [... lots of tabs ...] malicious code;

would appear identical to:

-old code;
+new good code;

Users who prefer the old behavior can still set the $LESS environment variable to -FRSXexplicitly, or set core.pager to 'less -S'.

默认情况下,Git 过去设置$LESS-FRSXif$LESS不是用户设置的。
这些FRX标志实际上对 Git 有意义(F并且X因为有时输出 Git 管道到 less 很短,并且R因为 Git 管道彩色输出)。
S标志(剁长线),在另一方面,是不相关的GIT中和是用户偏好的问题。Git 不应决定让用户更改LESS默认值

更具体地说,该S标志会损害在寻呼机中查看不受信任代码的用户,因为补丁如下所示:

-old code;
+new good code; [... lots of tabs ...] malicious code;

看起来与:

-old code;
+new good code;

喜欢旧行为的用户仍然可以将 $LESS 环境变量-FRSX显式设置为,或将 core.pager 设置为“ less -S”。

The documentation will read:

该文档将阅读:

The environment does not set the Soption but the command line does, instructing less to truncate long lines.
Similarly, setting core.pagerto less -+Fwill deactivate the Foption specified by the environment from the command-line, deactivating the "quit if one screen" behavior of less.
One can specifically activate some flags for particular commands: for example, setting pager.blameto less -Senables line truncation only for git blame.

环境不会设置S选项,但命令行会设置,指示 less 截断长行。
同样,设置core.pagerless -+F将停用的F选项,通过在命令行环境中指定,停用“ quit if one screen”的行为less
可以专门为特定命令激活一些标志:例如,设置pager.blameless -S仅对git blame.

回答by mtahmed

You can disable/enable pagers for specific outputs in the global configuration as well:

您也可以在全局配置中禁用/启用特定输出的寻呼机:

git config --global pager.diff false

Or to set the core.pager option, just provide an empty string:

或者设置 core.pager 选项,只需提供一个空字符串:

git config --global core.pager ''

This is better in my opinion than setting it to catas you say.

在我看来,这比将其设置cat为您所说的要好。

回答by StellarVortex

Regarding the disabled color when piping:

关于管道时禁用的颜色:

Use --colorto avoid that coloring is disabled.

使用--color以避免着色被禁用。

git diff --color | less -R

git diff --color | less -R

Or configure it forced on (in e.g. .gitconfig):

或者强制配置它(例如在 .gitconfig 中):

[color]
        ui = on

git diff | less -R

git diff | less -R

For non-color tools, then use:

对于非颜色工具,请使用:

git diff --no-color | some-primitive-tool

git diff --no-color | some-primitive-tool

Exporting environment variable LESS=-R(in e.g. .bashrc) turns on color support by default in "less":

导出环境变量LESS=-R(例如 .bashrc 中)在“less”中默认打开颜色支持:

git diff | less

git diff | less

回答by jcoffland

This worked for me with Git version 2.1.4 on Linux:

这对我在 Linux 上的 Git 版本 2.1.4 有用:

git config --global --replace-all core.pager cat

This makes Git use catinstead of less. catjust dumps the output of diffto the screen without any paging.

这使得 Git 使用cat而不是less. cat只是将 的输出转储diff到屏幕而不进行任何分页。

回答by Joseph Cheek

You can add an alias to diff with its own pager with pager.alias, like so:

您可以使用 pager.alias 使用自己的寻呼机为 diff 添加别名,如下所示:

[alias]
  dc = diff
  dsc = diff --staged
[pager]
  dc = cat
  dsc = cat

This will keep the color on and use 'cat' as the pager when invoked at 'git dc'.

这将保持颜色并在“git dc”调用时使用“cat”作为寻呼机。

Also, things notto do:

此外,不要做的事情:

  • use --no-pagerin your alias. Git (1.8.5.2, Apple Git-48) will complain that you are trying to modify the environment.
  • use a shell with !shor !git. This will bypass the environment error, above, but it will reset your working directory (for the purposes of this command) to the top-level Git directory, so any references to a local file will not work if you are already in a subdirectory of your repository.
  • --no-pager在您的别名中使用。Git (1.8.5.2, Apple Git-48) 会抱怨你试图修改环境。
  • 使用带有!sh或的外壳!git。这将绕过上面的环境错误,但它会将您的工作目录(出于此命令的目的)重置为顶级 Git 目录,因此如果您已经位于您的存储库。

回答by sekmo

I have this hunk in my .gitconfigand it seems to work fine (disabled for both diff and show):

我有这个大块头,.gitconfig它似乎工作正常(对 diff 和 show 都禁用):

[pager]
    diff = false
    show = false

回答by daniel kullmann

I like to disable paging from time to time, when I know the output is not very long. For this, I found a neat trick using Git aliases:

当我知道输出不是很长时,我喜欢不时禁用分页。为此,我发现了一个使用 Git 别名的巧妙技巧:

git config --global --add alias.n '!git --no-pager'

Or add the following to the [alias]section of ~/.gitconfig:

或者在[alias]~/.gitconfig 部分添加以下内容:

n = !git --no-pager

This means that you can use the prefix nto turn off paging for any Git command, i.e.:

这意味着您可以使用前缀n来关闭任何 Git 命令的分页,即:

git n diff # Show the diff without pager
git n log -n 3 # Show the last three commits without pager
git n show v1.1 # Show information about a tag