如何防止“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
How do I prevent 'git diff' from using a pager?
提问by Lakshman Prasad
Is there a command line switch to pass to git diff
and other commands that use the less
pager 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-pager
to Git will tell it to not use a pager. Passing the option -F
to less
will tell it to not page if the output fits in a single screen.
--no-pager
Git 会告诉它不要使用寻呼机。如果输出适合单个屏幕-F
,less
则传递选项将告诉它不分页。
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 -F
option to less
causes 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 -X
option 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-config
and 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:
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_PAGER
environment variable,- then
core.pager
configuration,- then
$PAGER
,- and then the default chosen at compile time (usually 'less').
When the
LESS
environment variable is unset, Git sets it toFRSX
(ifLESS
environment variable is set, Git does not change it at all).If you want to selectively override Git's default setting for
LESS
, you can setcore.pager
to e.g.less -+S
.
This will be passed to the shell by Git, which will translate the final command toLESS=FRSX less -+S
. The environment tells the command to set theS
option 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.pager
documentation
配置:重写core.pager
文档
The text mentions
core.pager
andGIT_PAGER
without giving the overall picture of precedence. Borrow a better description from thegit 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
LESS
is 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 b327583(Matthieu 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
$LESS
to-FRSX
if$LESS
was not set by the user.
TheFRX
flags actually make sense for Git (F
andX
because sometimes the output Git pipes to less is short, andR
because Git pipes colored output).
TheS
flag (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 changeLESS
's default.More specifically, the
S
flag 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
-FRSX
explicitly, or set core.pager to 'less -S
'.
默认情况下,Git 过去设置
$LESS
为-FRSX
if$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
S
option but the command line does, instructing less to truncate long lines.
Similarly, settingcore.pager
toless -+F
will deactivate theF
option specified by the environment from the command-line, deactivating the "quit if one screen
" behavior ofless
.
One can specifically activate some flags for particular commands: for example, settingpager.blame
toless -S
enables line truncation only forgit blame
.
环境不会设置
S
选项,但命令行会设置,指示 less 截断长行。
同样,设置core.pager
于less -+F
将停用的F
选项,通过在命令行环境中指定,停用“quit if one screen
”的行为less
。
可以专门为特定命令激活一些标志:例如,设置pager.blame
为less -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 cat
as you say.
在我看来,这比将其设置cat
为您所说的要好。
回答by StellarVortex
Regarding the disabled color when piping:
关于管道时禁用的颜色:
Use --color
to 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 cat
instead of less
. cat
just dumps the output of diff
to 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-pager
in your alias. Git (1.8.5.2, Apple Git-48) will complain that you are trying to modify the environment. - use a shell with
!sh
or!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 .gitconfig
and 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 n
to 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